上传文件并转换为 Base64(406 错误不可接受)
Upload File and Convert to Base64 (406 Error Not Acceptable )
我在这里尝试做的是将转换后的 pdf 发送到 base64 到端点,这里是端点
HTTP-HEADERS:
api-key: your-key
Content-Type: application/json
Request Body JSON
{
"file":
{
"mime": "application/pdf",
"data": "base64-data="
}
}
这是我上传和转换文件的方式
public async Task UploadFile()
{
FileData fileData = await CrossFilePicker.Current.PickFile();
if (fileData == null)
return; //user canceled selecting image
string fileName = fileData.FileName;
string contents = Encoding.UTF8.GetString(fileData.DataArray);
var stream = fileData.GetStream();
var bytes = new byte[stream.Length];
await stream.ReadAsync(bytes, 0, (int)stream.Length);
string base64 = Convert.ToBase64String(bytes);
File ru = new File();
ru.mime = "application/pdf";
ru.data = "base64-data="+base64;
string url = "ENDPOINT URL";
HttpClient client = new HttpClient();
client.DefaultRequestHeaders
.Accept
.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
string jsonData = JsonConvert.SerializeObject(ru);
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, url);
requestMessage.Content = new StringContent(jsonData, Encoding.UTF8, "application/json");
requestMessage.Headers.Add("api-key", "API KEY HERE");
requestMessage.Headers.Add("Accept-Encoding", "gzip");
HttpResponseMessage response = await client.SendAsync(requestMessage);
string result = await response.Content.ReadAsStringAsync();
if(result!= null)
{
resultLabel = result;
}
现在它给我错误
{"Message":"HTTP 406 Not Acceptable. There was an error with your request. Please check your payload and then try again,","Result":406}
按照@Jason 在评论区的建议我已经解决了我的问题
https://learning.postman.com/docs/sending-requests/generate-code-snippets/
希望这能解决和我有同样问题的人。
我在这里尝试做的是将转换后的 pdf 发送到 base64 到端点,这里是端点
HTTP-HEADERS:
api-key: your-key
Content-Type: application/json
Request Body JSON
{
"file":
{
"mime": "application/pdf",
"data": "base64-data="
}
}
这是我上传和转换文件的方式
public async Task UploadFile()
{
FileData fileData = await CrossFilePicker.Current.PickFile();
if (fileData == null)
return; //user canceled selecting image
string fileName = fileData.FileName;
string contents = Encoding.UTF8.GetString(fileData.DataArray);
var stream = fileData.GetStream();
var bytes = new byte[stream.Length];
await stream.ReadAsync(bytes, 0, (int)stream.Length);
string base64 = Convert.ToBase64String(bytes);
File ru = new File();
ru.mime = "application/pdf";
ru.data = "base64-data="+base64;
string url = "ENDPOINT URL";
HttpClient client = new HttpClient();
client.DefaultRequestHeaders
.Accept
.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
string jsonData = JsonConvert.SerializeObject(ru);
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, url);
requestMessage.Content = new StringContent(jsonData, Encoding.UTF8, "application/json");
requestMessage.Headers.Add("api-key", "API KEY HERE");
requestMessage.Headers.Add("Accept-Encoding", "gzip");
HttpResponseMessage response = await client.SendAsync(requestMessage);
string result = await response.Content.ReadAsStringAsync();
if(result!= null)
{
resultLabel = result;
}
现在它给我错误
{"Message":"HTTP 406 Not Acceptable. There was an error with your request. Please check your payload and then try again,","Result":406}
按照@Jason 在评论区的建议我已经解决了我的问题
https://learning.postman.com/docs/sending-requests/generate-code-snippets/
希望这能解决和我有同样问题的人。