Javascript-jquery+ajax File upload failed, what reason?

Source: Internet
Author: User
HTML section:

文件上传

Back-end PHP section: ajax.php


  
   

JS part, with JS upload time is successful, but with jquery when there are two kinds of errors:
One is to use the $.ajax method:

$(function(){        $(".sub").click(function(){             var fd=new FormData($("#up"));                    $.ajax({            url: 'ajax.php' ,            type: 'POST',            data: fd,            async: false,            cache: false,            contentType: false,           processData: false,            success: function (returndata) {                alert(returndata);            },            error: function (returndata) {                alert(returndata);            }       });              })})  

Error said can not find $_files in the pic, why?
Add: This place is done, Var fd=new FormData ($ ("#up")); Need to write:
var fd=new FormData ($ ("#up") [0]);
Specifically visible: http://segmentfault.com/q/1010000004213457
and http://segmentfault.com/a/1190000002938709

When using the $.post method:

$.post("ajax.php",fd,function(data){            console.log(data);         })

Error: uncaught typeerror:illegal invocation
Why is that?
(jquery is version 1.7.1)

Reply content:

HTML section:

文件上传

Back-end PHP section: ajax.php


  
   

JS part, with JS upload time is successful, but with jquery when there are two kinds of errors:
One is to use the $.ajax method:

$(function(){        $(".sub").click(function(){             var fd=new FormData($("#up"));                    $.ajax({            url: 'ajax.php' ,            type: 'POST',            data: fd,            async: false,            cache: false,            contentType: false,           processData: false,            success: function (returndata) {                alert(returndata);            },            error: function (returndata) {                alert(returndata);            }       });              })})  

Error said can not find $_files in the pic, why?
Add: This place is done, Var fd=new FormData ($ ("#up")); Need to write:
var fd=new FormData ($ ("#up") [0]);
Specifically visible: http://segmentfault.com/q/1010000004213457
and http://segmentfault.com/a/1190000002938709

When using the $.post method:

$.post("ajax.php",fd,function(data){            console.log(data);         })

Error: uncaught typeerror:illegal invocation
Why is that?
(jquery is version 1.7.1)

setting is not correct.

var data = new FormData();data.append('file', $('input[type=file]')[0].files[0]);$.ajax({        url: 'ajax.php',        data: data,        processData: false,        type: 'POST'        contentType: 'multipart/form-data',        mimeType: 'multipart/form-data',        success: function (data) {            alert(data);        }    });

Ajax is not able to upload attachments, if you need to upload the attachment, please refer to using jquery form plugin

Ajax can only transfer text streams and cannot transport binary files.

can refer to I write about formdata do not refresh the upload file

FormData

Ajax cannot upload files, and the enctype attribute of the form upload file is to be

      contentType: false,       processData: false,
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.