2 回答
TA贡献1834条经验 获得超8个赞
解决了!!!
通过获取价值
image = $("#input-file-img").val()
这意味着我正在将类型字符串作为文件发送
所以我不得不把它改成
image = $('#input-file-img')[0].files[0]
一切都很好
TA贡献1806条经验 获得超8个赞
我认为你的服务器端代码很好,如果我修改客户端代码如下,一切正常,我们最终在 /uploads 文件夹中得到图像:
function base64toBlob(base64, mimeType) {
const bytes = atob(base64.split(',')[1]);
const arrayBuffer = new ArrayBuffer(bytes.length);
const uintArray = new Uint8Array(arrayBuffer);
for (let i = 0; i < bytes.length; i++) {
uintArray[i] = bytes.charCodeAt(i);
}
return new Blob([ arrayBuffer ], { type: mimeType });
}
function submitForm() {
const imgRegEx = /^data:(image\/(gif|png|jpg|jpeg))/;
const imageData = $('#input-file-img').attr('src');
const mimeType = imgRegEx.exec(imageData)[1];
const blob = base64toBlob(imageData, mimeType);
const fileExt = mimeType.replace("image/", "");
const fileName = "test-image." + fileExt; // Change as appropriate..
const data = new FormData();
data.append("image", blob, fileName);
$.ajax({
url: '/uploadfile/' + userName,
method: 'POST',
async: false,
processData: false ,
contentType: false,
data: data
})
}
添加回答
举报