简体   繁体   English

XmlHttpRequest PUT文件上传停止> 1MB

[英]XmlHttpRequest PUT File Upload stopping > 1MB

I am uploading files to a server (in this case, S3). 我正在将文件上传到服务器(在本例中为S3)。 Any file under ~1MB works fine. 小于1MB的任何文件都可以正常工作。 Files above that size usually fail, although not every time. 超过该大小的文件通常会失败,尽管并非每次都会失败。 I have the process working for the small files, so the issue does not seem to lie in security, CORS, signing, etc. It seems to be during the send. 我有处理小型文件的过程,因此问题似乎不在于安全性,CORS,签名等。这似乎是在发送过程中。

Small files work ~100% but even larger files do not fail immediately, but fire a couple of progress events that show correct total byte and increasing loaded byte counts as they should. 小文件可以工作约100%,但更大的文件不会立即失败,但是会触发几个进度事件,这些事件显示正确的总字节数和应有的增加的加载字节数。 However, after a few of these progress events, it fires the error and the send stops. 但是,在一些此类进度事件之后,它将触发错误并停止发送。 Once in while, a file of 7-8MB has succeeded, but the same file will fail on other attempts. 偶尔会成功发送7-8MB的文件,但相同的文件将在其他尝试下失败。

Here are the relevant lines of code with as much fluff removed as possible. 以下是相关的代码行,其中删除了尽可能多的绒毛。 The url is a signed temporary PUT request that is working and passing the OPTIONS preflight call that precedes the PUT call. 该url是一个已签名的临时PUT请求,该请求正在运行并通过在PUT调用之前的OPTIONS预检调用。 The content-type header is required as it gets included in the signing process for the url. 内容类型标头是必需的,因为它包含在url的签名过程中。

xhr = new XMLHttpRequest()
xhr.upload.onerror = (e) ->
  console.log(xhr)
  console.log(e)
xhr.onerror = (e) ->
  console.log(xhr)
  console.log(e)
  true
xhr.open('PUT', url)
xhr.setRequestHeader('content-type', file.type)
xhr.send(file)

I have tried different files, sizes, extensions and those do not seem to form any pattern other than size. 我尝试了不同的文件,大小,扩展名,这些文件似乎没有形成大小以外的任何模式。 The size of the file is an indicator but not exact. 文件的大小是一个指标,但不准确。 Based on that, I looked for timeouts and made them large (just in case). 在此基础上,我寻找超时并将其增大(以防万一)。 I did try to catch onabort and ontimeout and neither one is fired. 我确实尝试过onabort和ontimeout,但都没有被解雇。

When I get the error, I can find nothing o the event, xhr or xhr.upload to indicate why it stopped. 当我得到错误时,在事件xhr或xhr.upload中什么也找不到,以指示其停止的原因。

Chrome tools shows the attempt as a PUT with status '(failed).' Chrome工具将尝试显示为状态为“(失败)”的PUT。

The error event shows type = "error" but no other information. 错误事件显示类型=“错误”,但没有其他信息。 The xhr at that point shows nothing (readyState: 4, response: "", responseText: "", responseType: "", responseXML: null, status: 0, statusText: "") 此时的xhr不显示任何内容(readyState:4,响应:“”,responseText:“”,responseType:“”,responseXML:null,状态:0,statusText:“”)

If anyone has any words of wisdom to offer or even a far-fetched idea to try, I would appreciate it. 如果有人可以提供任何智慧的话,甚至可以尝试一个牵强的想法,我将不胜感激。 Thanks. 谢谢。

Maybe it's not a problem of ajax request, but a problem of configuration of the server. 也许这不是ajax请求的问题,而是服务器配置的问题。 Your server must accept file uploads maximum size of 1Mb. 您的服务器必须接受最大1Mb的文件上传。

If you use php set upload_max_filesize and post_max_size in your php.ini. 如果您使用php,请在php.ini中设置upload_max_filesizepost_max_size

/* For example */
upload_max_filesize = 10M
post_max_size = 10M

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

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