我正在研究一个允许用户将图像和数据提交到我们的服务器的Web应用程序(使用JQuery版本2.2.4)。当用户决定上传他们的提交内容时,我的代码应该使用JSZip库生成一个zip文件并使用POST将其上传到我们的服务器。某些搜索这里StackExchange后,我想出了这个代码:在Javascript中使用POST上传zip文件失败,无提示
var zip = new JSZip(); // Create the object representing the zip file
// ...Add the data and images
console.log('Generating compressed archive...');
zip.generateAsync({
compression: 'DEFLATE',
type: 'blob'
}).then(function(zc) {// Function called when the generation is complete
console.log('Compression complete!');
// Create file object to upload
var fileObj = new File([zc], fileName);
console.log('File object created:', fileObj);
$.post('http://myurl/submit', {
data: fileObj,
}).done(function() {
console.log('Ajax post successful.');
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.log('Ajax post failed. Status:', textStatus);
console.log(errorThrown);
});
});
我的代码打印File对象创建消息,文件对象本身看起来不错,但后来我得到没有别的。沉默失败。 POST调用甚至不出现在Firebug的Net面板中。
更多的搜索后,我也尝试添加该代码事先:
$(document).ajaxError(function(event, jqxhr, settings, thrownError) {
console.log('Ajax post failed. Event:', event);
console.log('Ajax settings:', settings);
console.log(thrownError);
});
但是,这并不被触发。在设置错误回调方面显然存在一些错误 - 我可以尝试什么?
随着您的修改,我得到一个HTTP错误400(错误的请求)。 – Btz
您链接的答案对解决我的问题非常有帮助!我创建了一个新的答案,以便我可以放置代码。 – Btz