简体   繁体   English

从Javascript上传文件

[英]Upload file from Javascript

I'm developing a Javascript app and I need to use the Mediafire REST API to upload a file. 我正在开发Javascript应用程序,需要使用Mediafire REST API上载文件。 (See upload ducmentation here ). (请参见此处的上传教育)。

Following this MDN tutorial , and some research, I've written the code below, but it seems it's not working... Note that I don't need for the moment to control the progress and so on, I only want to do the most basic upload operation possible... 遵循此MDN教程和一些研究之后,我编写了下面的代码,但似乎没有用...请注意,我暂时不需要控制进度,等等,我只想做最基本的上传操作可能...

Also note that I could a different code, and even jQuery or other (free) libraries, so if you have a better code to upload a file I'd be really grateful... 还要注意,我可以使用不同的代码,甚至可以使用jQuery或其他(免费)库,因此,如果您有更好的代码来上传文件,我将不胜感激。

var controller = this;
var file = $("#file").get(0).files[0];
//The file is correctly retrieved here...

var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://www.mediafire.com/api/upload/upload.php?session_token=' + controller.sessionToken.token);
//(The session_token is valid)
xhr.overrideMimeType('text/plain; charset=x-user-defined');
xhr.setRequestHeader('x-filesize', file.size);

var reader = new FileReader();
reader.onload = function (evt) {
  var uInt8Array = new Uint8Array(evt.target.result);
  //It seems that here the ArrayBuffer is read correctly, 
  //and I converted it to a ArrayBufferView because Chrome suggested it...
  xhr.send(uInt8Array);
};
reader.readAsArrayBuffer(file);

I can't tell the concrete error, I only know that nothing is happens... but maybe looking at the code you can see some obvious error... The only thing I see is this in Chrome's console: 我不能说出具体的错误,我只知道什么也没发生...但是也许看一下代码,您会看到一些明显的错误...我唯一看到的是在Chrome控制台中的错误:

在此处输入图片说明

在此处输入图片说明

Note: I know the quality of this question is not the desired and it's TOO vague, but I tried to do my best taking into account that I'm completely new to ALL these technologies... 注意:我知道这个问题的质量不是所期望的,而且太模糊了,但是我尝试着尽我所能,因为我对所有这些技术都是全新的...

The presence of an OPTIONS request and the presence of specific headers in the OPTIONS request indicates that your POST request is a cross-domain request. OPTIONS请求的存在以及OPTIONS请求中特定标头的存在指示您的POST请求是跨域请求。 The user agent, following the CORS spec , is first sending an OPTIONS request. 遵循CORS规范的用户代理首先发送OPTIONS请求。 This is also called a preflight request, and it is sent, in your case, due to the non-standard header you are including (x-filesize) and the fact that the Content-Type is not form-encoded, MPE, or text/plain. 这也称为预检请求,在您的情况下,由于所包含的标头(x-filesize)以及内容类型未经格式编码,MPE或文本的事实,因此发送该请求/普通。 You can either deal with the OPTIONS request server-side, or make appropriate changes to your request so that a preflight is not required. 您可以在服务器端处理OPTIONS请求,也可以对您的请求进行适当的更改,这样就不需要进行飞行前检查。 Either way, you are going to have to deal with CORS requests server-side since you are apparently making a cross-domain request. 无论哪种方式,您都将不得不在服务器端处理CORS请求,因为您显然是在进行跨域请求。 You can read more about CORS in this excellent MDN article . 您可以在这篇出色的MDN文章中了解有关CORS的更多信息。

PS Why are you using FileReader here? PS:为什么在这里使用FileReader? Just send the File object via XHR, or, better yet, append the File to a FormData object and send that. 只需通过XHR发送File对象,或者更好的是,将File附加到FormData对象并发送。

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

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