xmlhttprequest上传报错error: [object ProgressEvent]
写web拖拽上传的app时遇到了一个问题,用xmlhttprequest上传图片总是报错,error: [object ProgressEvent]检查了一下,其他代码没问题,问题就出在用xmlhttprequest上传的代码上。。我用了好多方法,可老是这同一个错误。。用过的代码如下:
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:8888/upload");
xhr.setRequestHeader("X-Requested-With","XMLHttpRequest");
var fd = new FormData();
fd.append('mypic',file);
xhr.send(fd);
还有这个:
var reader = new FileReader();
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:8888/upload");
xhr.overrideMimeType("application/octet-stream");
xhr.sendAsBinary(reader.readAsBinaryString(file));
和这个:
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:8888/upload");
var boundary="---";
xhr.setRequestHeader("Content-Type", "multipart/form-data, boundary="+boundary);
var body = '';
body += "--" + boundary + "\r\n";
body += "Content-Disposition: form-data; name=\""+dropbox.getAttribute('name')+"\"; filename=\"" + file.Name + "\"\r\n";
body += "Content-Type: "+file.Type+"\r\n\r\n";
body += file.Data + "\r\n";
body += "--" + boundary + "--\r\n";
xhr.sendAsBinary(body);
三种代码,运行时总是同一个错误error: [object ProgressEvent],而且不管握开不开服务器都一样,后端表示没有收到数据。。我猜问题是前端的,并且是执行send语句时出的问题,但到底是为什么啊??
为了万一,把服务器端的代码也贴出来:
function upload(response, request) {
console.log("Request handler 'upload' was called.");
var form = new formidable.IncomingForm();
console.log("about to parse");
form.parse(request, function(error, fields, files) {
console.log("parsing done");
fs.renameSync(files.upload.path, "/home/veta/Desktop/upload/tmp/test.png");
response.writeHead(200, {"Content-Type": "text/html"});
response.write("received image:<br/>");
response.write("<img src='/show'/>");
response.end();
});}
小白一个,若是闹了什么笑话,还请不要介意。。。
node.js web前端开发 Ajax JavaScript
newday
11 years, 8 months ago