我正在使用JavaScript中的文件阅读器,我需要将我的图像上传到WebApi中,并将其转换成字节数组并保存在服务器中,其工作正常,现在我的问题是base64字符串增加了图像的大小,假设我上传了30 it的图像,它在服务器中存储了389 it,如何将图像保存在相同大小或缩小图像大小需要帮助。
//File Reader
function OnFileEditImageEntry(file) {
var reader = new FileReader();
reader.onloadend = function (evt) {
var ImageBase64 = evt.target.result;
return ImageBase64 ;
};
reader.readAsDataURL(file);
}
//WEB API//
public IHttpActionResult UpdateUserDetails(ImageModel model)
{
try
{
if (model.ImageBase64 != "")
{
var PicDataUrl = "";
string ftpurl = "ftp://xxx.xxxxx.xxxx/";
var username = "xxx";
var password = "xxxxx";
string UploadDirectory = "xxxx/xx";
string FileName =model.ImageFileName;
String uploadUrl = String.Format("{0}{1}/{2}", ftpurl, UploadDirectory,FileName);
FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(uploadUrl);
req.Proxy = null;
req.Method = WebRequestMethods.Ftp.UploadFile;
req.Credentials = new NetworkCredential(username, password);
req.EnableSsl = false;
req.UseBinary = true;
req.UsePassive = true;
byte[] data =Convert.FromBase64String(model.ImageBase64);
req.ContentLength = data.Length;
Stream stream = req.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
}
}
}发布于 2017-01-12 22:17:34
用base64 64/FileReader发送原始二进制文件,而不是增加30%的大小
带取
// sends the raw binary
fetch('http://example.com/upload', {method: 'post', body: file})
// Append the blob/file to a FormData and send it
var fd = new FormData()
fd.append('file', file, file.name)
fetch('http://example.com/upload', {method: 'post', body: fd})用XHR
// xhr = new ...
// xhr.open(...)
xhr.send(file) // or
xhr.send(fd) // send the FormData通常,当上传文件时,尽量避免发送json,因为很多开发人员都会出错。json中的二进制数据等于错误的实践(以及更大的大小),例如:
$.post(url, {
name: '',
data: base64
})尽可能多地使用FormData#append,或者如果您愿意的话:
fd.append('json', json)https://stackoverflow.com/questions/41610480
复制相似问题