gpt4 book ai didi

javascript - Angular 表单上传在 Safari 中不起作用,在 Chrome 中起作用

转载 作者:行者123 更新时间:2023-11-28 05:23:27 26 4
gpt4 key购买 nike

我尝试使用 Angular $http 上传文件。

它在 Chrome 中正常工作,甚至在 Safari 中发送请求(并且有来自服务器的 200 响应),但文件未上传到服务器。

我也尝试使用 ngUpload 库,但结果相同 - 文件尚未上传到服务器。

源代码:

var formData = new FormData();
formData.append('userid', Users.getCurrentId());
formData.append('avatar', myFile); // this is File() size abput 100K

$http({
url: AppState.getApiHost()+AppState.getApiPrefix() + '/setavatar',
method: "POST",
data: formData,
headers: {'Content-Type': undefined}
})
.then(function (res) {
console.log('Success', res);
})
.catch(function (err) {
console.log('Error',err)
});

最佳答案

我刚刚找到了答案。问题在于 File() 对象是使用 File() 构造函数创建的:

return new File([u8arr], filename, {type:mime}); 

但它在 Safari 上无法正常工作。它创建大小与文件大小相同的 File 对象,但实际文件为空。如果我尝试在附加到表单后发送它 - 表单已发送空文件(即使它看起来不是空文件)。

我已将其替换为以下代码:

var the_blob = new Blob([u8arr]);
the_blob.lastModifiedDate = new Date();
the_blob.name = 'avatar.png'

return the_blob;

这对我来说是工作

关于javascript - Angular 表单上传在 Safari 中不起作用,在 Chrome 中起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40358951/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com