有人会知道RestSharp的含义吗?
request.AddParameter("text/plain", body, ParameterType.RequestBody);
使用Javascript?那是我唯一想念的部分。
这是C#中的有效代码:
public static void UploadFile(string bearer)
{
var client = new RestClient("...");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Bearer " + bearer);
request.AddHeader("cache-control", "no-cache");
var imageLenngth = new FileInfo(@"...").Length;
request.AddHeader("Content-Type", "image/jpeg");
request.AddHeader("Content-Length", imageLenngth.ToString());
byte[] body = File.ReadAllBytes(@"...");
request.AddParameter("text/plain", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
}
并且在Ajax JavaScript中代码不能完全正常工作(它会上传到云服务器,但是上传的图像不正确。插入损坏的图像的字节数太多了)
function uploadingImage(binaryImage) {
var settings = {
"async": true,
"crossDomain": true,
"url": "...",
"method": "POST",
"headers": {
"Content-Type": "image/jpeg",
"Authorization": "Bearer ...",
"cache-control": "no-cache",
},
"data": binaryImage
}
$.ajax(settings).done(function (response) {
console.log(response);
});
}
答案 0 :(得分:0)
我发现了问题。提琴手帮助调查了发送的内容。默认情况下,传入的数据将被序列化。为防止这种情况,我们需要明确告知不要序列化它。 “ processData”:假的把戏。这是工作代码:
function uploadingImage(binaryImage) {
var settings = {
"async": true,
"crossDomain": true,
"url": "...",
"method": "POST",
"headers": {
"Content-Type": "image/jpeg",
"Authorization": "Bearer ...",
"cache-control": "no-cache",
},
"processData":false,
"data": binaryImage
}
$.ajax(settings).done(function (response) {
console.log(response);
});
}