在 Javascript 中使用 POST 上传 zip 文件无提示地失败
Uploading a zip file using POST in Javascript fails silently
我正在开发一个 Web 应用程序(使用 JQuery 版本 2.2.4),它允许用户向我们的服务器提交图像和数据。当用户决定上传他们提交的内容时,我的代码应该使用 JSZip 库生成一个 zip 文件并使用 POST 将其上传到我们的服务器。在 StackExchange 上进行一些搜索后,我想到了这段代码:
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 object created 消息,file object 本身看起来没问题,但我什么也没得到。无声的失败。 POST 调用甚至没有出现在 Firebug 的网络面板中。
经过更多的搜索,我也尝试预先添加这段代码:
$(document).ajaxError(function(event, jqxhr, settings, thrownError) {
console.log('Ajax post failed. Event:', event);
console.log('Ajax settings:', settings);
console.log(thrownError);
});
但这并没有被触发。显然我在设置错误回调时犯了一些错误 - 我可以尝试什么?
我认为您没有看到任何 POST
,因为您的数据对象不仅仅包含字符串值(如果我使用 {data: "content"}
,我会得到一个 POST
)。
关注 and , you need to add some parameters (documentation):
$.post({
url: "/test",
data: fileObj,
contentType: false,
processData: false
})
我已经设法让上传开始工作,创建一个 FormData 对象并将我的文件粘贴到其中。这是代码:
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);
var fd = new FormData();
fd.append('fileName', fileName);
fd.append('file', fileObj);
fd.append('mimeType', 'application/zip');
// POST Ajax call
$.ajax({
type: 'POST',
url: 'http://myurl/submit',
data: fd,
contentType: false,
processData: false,
}).done(function() {
console.log('Ajax post successful.');
}).fail(function(jqXHR, textStatus, errorThrown) {
console.log('Ajax post failed. Status:', textStatus);
console.log(jqXHR);
console.log(errorThrown);
});
});
这是受 David Duponchel 链接到的其他 StackExchange 答案的启发。
我正在开发一个 Web 应用程序(使用 JQuery 版本 2.2.4),它允许用户向我们的服务器提交图像和数据。当用户决定上传他们提交的内容时,我的代码应该使用 JSZip 库生成一个 zip 文件并使用 POST 将其上传到我们的服务器。在 StackExchange 上进行一些搜索后,我想到了这段代码:
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 object created 消息,file object 本身看起来没问题,但我什么也没得到。无声的失败。 POST 调用甚至没有出现在 Firebug 的网络面板中。
经过更多的搜索,我也尝试预先添加这段代码:
$(document).ajaxError(function(event, jqxhr, settings, thrownError) {
console.log('Ajax post failed. Event:', event);
console.log('Ajax settings:', settings);
console.log(thrownError);
});
但这并没有被触发。显然我在设置错误回调时犯了一些错误 - 我可以尝试什么?
我认为您没有看到任何 POST
,因为您的数据对象不仅仅包含字符串值(如果我使用 {data: "content"}
,我会得到一个 POST
)。
关注 and , you need to add some parameters (documentation):
$.post({
url: "/test",
data: fileObj,
contentType: false,
processData: false
})
我已经设法让上传开始工作,创建一个 FormData 对象并将我的文件粘贴到其中。这是代码:
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);
var fd = new FormData();
fd.append('fileName', fileName);
fd.append('file', fileObj);
fd.append('mimeType', 'application/zip');
// POST Ajax call
$.ajax({
type: 'POST',
url: 'http://myurl/submit',
data: fd,
contentType: false,
processData: false,
}).done(function() {
console.log('Ajax post successful.');
}).fail(function(jqXHR, textStatus, errorThrown) {
console.log('Ajax post failed. Status:', textStatus);
console.log(jqXHR);
console.log(errorThrown);
});
});
这是受 David Duponchel 链接到的其他 StackExchange 答案的启发。