简体   繁体   English

Ajax:如何使用FormData和jQuery发送“空”文件并在$ _FILES中获取

[英]Ajax: How to send 'empty' file using FormData and jQuery and get it in $_FILES

I have html form and want send file 'myfile' with Ajax and jQuery: 我有HTML表单,并希望使用Ajax和jQuery发送文件“ myfile”:

<form method="post" enctype="multipart/form-data">
    <input type="file" name="myfile" id="myfile">
</form>

My javascript code: 我的JavaScript代码:

function getFile(fieldName){
    var itemValue = $('input[name="' + fieldName + '"]')[0].files[0];
    return itemValue;
}

function sendFile(){
    var formaData = new FormData();
    var itemName = 'myfile';
    var itemValue = getFile(itemName);
    formaData.append(itemName, itemValue);
    $.ajax({
        type: 'POST',
        data: formaData,
        processData: false,
        contentType: false,
    });
}

All works fine when file is choosen. 选择文件后,一切正常。 I have in the server $_FILES global array and data in it which got via Ajax: 我在服务器中有$ _FILES全局数组和通过Ajax获得的数据:

["myfile"]=>
    array(5) {
        ["name"]=>
        string(13) "file_name.jpg"
        ["type"]=>
        string(10) "image/jpeg"
        ["tmp_name"]=>
        string(14) "/tmp/phpefJlk3"
        ["error"]=>
        int(0)
        ["size"]=>
        int(344100)
    }

The problem when file is not choosen! 未选择文件时的问题! When I don't choose any file and send Ajax request then I have empty $_FILES, but I have $_POST with field 'myfile' equals 'undefined'. 当我不选择任何文件并发送Ajax请求时,我有空的$ _FILES,但是我有$ _POST且字段“ myfile”等于“ undefined”。 But I need 'myfile' field in $_FILES even it is empty. 但是我需要$ _FILES中的'myfile'字段,即使它为空。 I want something like: 我想要类似的东西:

["myfile"]=>
    array(5) {
        ["name"]=>
        string(0) ""
        ["type"]=>
        string(0) ""
        ["tmp_name"]=>
        string(0) ""
        ["error"]=>
        int(4)
        ["size"]=>
        int(0)
    }

Above is what I have in $_FILES globall array if I send empty form without Ajax. 如果我不使用Ajax发送空表单,则上面是$ _FILES globall数组中的内容。 I want the same if I using Ajax. 如果我使用Ajax,我也希望如此。 Is it posssible? 有可能吗?

you could check for $_FILES['myfile']['error'] and $_FILES['myfile']['size'] and return an array of your choice as result. 您可以检查$_FILES['myfile']['error']$_FILES['myfile']['size']并返回您选择的数组作为结果。 if $_FILES['myfile']['error'] is equal to zero then there is no error. 如果$ _FILES ['myfile'] ['error']等于零,则没有错误。 you can find more details on php.net 您可以在php.net上找到更多详细信息

Handling files with AJAX is not that simple. 使用AJAX处理文件不是那么简单。 It comes up with a lot of problems due to different clients, hugh files and all that stuff. 由于不同的客户端,庞大的文件以及所有这些东西,它带来了很多问题。 (As you can see in your "simple"-Error here =) In your case, no file is selected so there is no need to send information about "no given file". (如您在“简单”中看到的-这里错误=)。在您的情况下,未选择文件,因此无需发送有关“未给定文件”的信息。 Dont produce overheads. 不要产生开销。 Maybe you take a look into your controller. 也许您看看您的控制器。 If the data is that different (AJAXPOST or POST) so create two PHP-Actions. 如果数据不同(AJAXPOST或POST),则创建两个PHP-Action。 One AJAX handler and one POST handler. 一个AJAX处理程序和一个POST处理程序。 In that case you can handle the different post-datas. 在这种情况下,您可以处理不同的后数据。

This information will help you: How can I upload files asynchronously? 这些信息将为您提供帮助: 如何异步上传文件?

You need to use Plugins like Uploadify , or Valums to make that work. 您需要使用UploadifyValums之类的插件才能使该工作正常。 Just read a bit more about AJAX and Filepost. 只需阅读更多有关AJAX和Filepost的内容。

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

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