在XmlHttprequest(javascript)中发送生成的(JSZip)zip文件 [英] Sending generated (JSZip) zip file in XmlHttprequest (javascript)

查看:367
本文介绍了在XmlHttprequest(javascript)中发送生成的(JSZip)zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了一个Web应用程序,在该应用程序中,我根据用户输入创建了一组文件,使用JSZip库将其压缩,然后尝试使用Xhr将文件发布到服务器.代码如下:

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. 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);

我与这段代码的问题是永远不会调用onload函数.我以为这是因为我的函数在请求的要求之前退出了,所以我在参数中将Xhr请求同步设置设置为false.这导致了NS_ERROR.我读到Xhr无法在遥远的域上执行请求.真的吗 ?您将如何解决此问题. ? 从服务器端(我要发布到该端)的角度来看,已接收到文件并返回了json响应.似乎客户端从未收到响应.

My problem with this piece of code is that the onload function is never called. 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. This resulted in a NS_ERROR. I read that Xhr cannot perform request on distant domain. 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. It just seems that the client side never receive the response.

感谢您的帮助!

推荐答案

使用此

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);

这篇关于在XmlHttprequest(javascript)中发送生成的(JSZip)zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆