简体   繁体   English

在XmlHttprequest(javascript)中发送生成的(JSZip)zip文件

[英]Sending generated (JSZip) zip file in XmlHttprequest (javascript)

i'am design a web application in which i create a set of file from user input, zip them using the JSZip library and then try to post the files to a server using Xhr. 我正在设计一个Web应用程序,在该应用程序中,我根据用户输入创建了一组文件,使用JSZip库将其压缩,然后尝试使用Xhr将文件发布到服务器。 The code is the following : 代码如下:

var content = zip.generate({type : "blob"});
var server_path = document.getElementById('build_server').value;
server_path = server_path + '/upload_project';
console.log(server_path);
global_oreq = new XMLHttpRequest();
var file_name = design_name + '.zip';
var formData = new FormData();
var json_build_answer = '';
global_oreq.open("POST", server_path, true);
global_oreq.onload = function(e) {
    // json_build_answer = JSON.parse(oReq.responseText);
    console.log('test');
    console.log(global_oreq.responseText);
};
formData.append('file', content, file_name);
global_oreq.send(formData);

My problem with this piece of code is that the onload function is never called. 我的这段代码的问题是永远不会调用onload函数。 I assumed that this is because my function quits before the ned of the request so i turned the Xhr request synchronous setting false in the arguments. 我以为这是因为我的函数在请求的要求之前退出了,所以我在参数中将Xhr请求同步设置设置为false。 This resulted in a NS_ERROR. 这导致了NS_ERROR。 I read that Xhr cannot perform request on distant domain. 我读到Xhr无法在远程域上执行请求。 Is that true ? 真的吗 ? What would you do to address this problem. 您将如何解决此问题。 ? From what is see on the server side (the one i'am posting to) is that the file is received and the json response returned. 从服务器端(我要发布到该端)的角度来看,是接收到文件并返回json响应。 It just seems that the client side never receive the response. 似乎客户端从未收到响应。

Thanks for your help ! 谢谢你的帮助 !

use this 用这个

var content = zip.generate({type : "blob"});
var server_path = document.getElementById('build_server').value;
server_path = server_path + '/upload_project';
console.log(server_path);
var global_oreq = new XMLHttpRequest();
var file_name = design_name + '.zip';
var formData = new FormData();
var json_build_answer = '';
global_oreq.open("POST", server_path, true);
global_oreq.onload = function(e) {
    // json_build_answer = JSON.parse(oReq.responseText);
console.log('test');
console.log(global_oreq.responseText);
};
formData.append('file', content, file_name);
global_oreq.send(formData);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM