简体   繁体   English

通过Ajax提交文件上传

[英]Submit file upload via Ajax

I'm trying to upload a binary file to a server, but avoid a complete page refresh when the server responds. 我正在尝试将二进制文件上传到服务器,但是请避免在服务器响应时刷新整个页面。 I'm far from experienced at this type of thing so everything about the way I'm doing it is subject to correction. 我对这种事情还没有足够的经验,所以我做事的方式都需要纠正。

This is what I have done. 这就是我所做的。

I have HTML like this: 我有这样的HTML:

<form id='form'>
    <input type='file' id='filename'></input>
    <input type='submit' onclick='doTheBusiness'></input>
</form>

I have javascript like this: 我有这样的JavaScript:

function doTheBusiness () {
    var url = '/target.html?arg1=' + val1 + '&arg2=' + val2;
    var fd = new FormData ($('#filename')[0]);
    $.each (
        $.ajax ({
             'url':         url,
             'type':        'post',
             'processData': false,
             'contentType': 'multipart/form-data',
             'success':     success-routine,
             'data':        {
                 'formData': fd
             }
        });
}

Before I added 'processData': false , I was getting an exception from Firefox and Chrome that appeared to be caused because the ajax function was trying to serialise my FormData object. 在添加'processData': false ,我从Firefox和Chrome获得了一个异常,该异常似乎是由于ajax函数试图序列化FormData对象而引起的。 When I looked into resolving that, I found that if I didn't have 'contentType': 'multipart/form-data' it would default to application/x-www-form-urlencoded and I didn't think that would be any use for the file contents. 当我解决这个问题时,我发现如果我没有'contentType': 'multipart/form-data' ,它将默认为application/x-www-form-urlencoded ,我认为这不会用于文件内容。 Once I'd done all that, I found that I couldn't access my arg1 and arg2 parameters on the server if I put them into the 'data' object as I have done in other places, so I hand-encoded them into the URL. 完成所有这些操作后,我发现,如果像我在其他地方所做的那样将它们放入'data'对象中,就无法访​​问服务器上的arg1arg2参数,因此我将它们手工编码为URL。

Unfortunately it's not working. 不幸的是,它不起作用。 Using Firefox, I now don't see the request at the server. 使用Firefox,我现在在服务器上看不到该请求。 Chrome and IE11 send a request, but I get an Internal apreq error when I call apreq_body to see if I can get to the uploaded file data. Chrome和IE11发送了一个请求,但是当我调用apreq_body来查看是否可以获取上载的文件数据时,出现Internal apreq error I've been through the mod_apreq code, and found that Internal apreq error corresponds to APREQ_ERROR_GENERAL , but that error seems to be just what it says, a general error code and I didn't like the idea of trying to follow all the possible places where it might be generated. 我遍历了mod_apreq代码,发现Internal apreq error对应于APREQ_ERROR_GENERAL ,但是该错误似乎就是它所说的,是一般错误代码,我不喜欢尝试遵循所有可能的位置的想法它可能在哪里生成。

(I've already seen How to send FormData objects with Ajax-requests in jQuery? which pointed me towards the 'processData' and 'contentType' properties.) (我已经看过如何在jQuery中发送带有Ajax请求的FormData对象?这使我指向了'processData''contentType'属性。)

I speculate that I am constructing the request wrongly, and it's so wrong that Firefox is (silently) refusing to send it. 我推测我在错误地构造了请求,并且Firefox(无声地)拒绝发送请求是非常错误的。 Chrome and IE11 are sending the request, but it's then not possible for Apache to parse it. Chrome和IE11正在发送请求,但是Apache无法解析该请求。 (It might be that this is what caused my problems trying to access the parameters and I can move them back to the 'data' object once I manage to send the file contents correctly.) (这可能是导致尝试访问参数的问题的原因,一旦设法正确发送文件内容,便可以将其移回'data'对象。)

Please help me to correct this code and allow me to upload binary files without any more external scripts apart from vanilla jQuery; 请帮助我更正此代码,并允许我上载二进制文件,除了香草jQuery之外,无需其他任何外部脚本; I suspect that even upgrading to a version later than jquery.1.7.2.min.js would be a step too far for my employers. 我怀疑即使升级到jquery.1.7.2.min.js以后的版本对于我的雇主来说也太过分了。

  • processData and contentType needs to be false processData和contentType必须为false
  • The FormData constructor takes a form as a parameter not a file input. FormData构造函数将表单作为参数而不是文件输入。

     var fd = new FormData ($('#form')[0]); 
  • Pass the form data object as the data parameter. 将表单数据对象作为数据参数传递。

     'data': fd, 

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

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