Why does javascript-jQuery + ajax File Upload fail?

Source: Internet
Author: User
Html: {code ...} backend php part: ajax. php {code ...} the js part is successfully uploaded using js, but there are two errors when using jquery: one is using $. ajax method: {code ...} the error message says the pic in $ _ FILES cannot be found. Why? Supplement: part of... html:

File Upload

Backend php part: ajax. php


  

The js part is successfully uploaded using js, but two errors occur when jquery is used:
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);            }       });              })})  

The error message says the pic in $ _ FILES cannot be found. Why?
Supplement: This is done. var fd = new FormData ($ ("# up"); it must be written:
Var fd = new FormData ($ ("# up") [0]);
Specific 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?
(Jquery is in version 1.7.1)

Reply content:

Html section:

File Upload

Backend php part: ajax. php


  

The js part is successfully uploaded using js, but two errors occur when jquery is used:
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);            }       });              })})  

The error message says the pic in $ _ FILES cannot be found. Why?
Supplement: This is done. var fd = new FormData ($ ("# up"); it must be written:
Var fd = new FormData ($ ("# up") [0]);
Specific 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?
(Jquery is in version 1.7.1)

Incorrect settings.

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 cannot upload attachments. If you need to upload attachments, see use the jQuery form plug-in.

Ajax can only transmit text streams, but cannot transport binary files.

For more information, see what I wrote about FormData not refreshing uploaded files.

FormData

Ajax cannot upload files, and the enctype attribute of form upload files is required.

      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.