PHP + Ajax
Script function ajaxFileUpload () {$. ajax ({url: 'A. php ', type: 'post', dateType: 'json', data: {upfile: $ ("# ss "). val ()}, success: function (result) {alert ("success") ;}, error: function (result) {alert ("error ");}});} script A. php if (! Is_uploaded_file ($ _ FILES ["upfile"] ["tmp_name"]) // If I directly submit a form $ _ FILES with a value (Array ([upfile] => Array ([name] => 1.jpg [type] => image/jpeg [tmp_name] => C: \ Windows \ phpC106.tmp [error] => 0 [size] => 7108) // but when I use ajax to submit, $ _ FILES has no value. how can I solve this problem? {Echo "the image does not exist! "; Exit ;}
Reply to discussion (solution)
Directly reading the value of the type = file control cannot upload files.
To upload an ajax file, you must use a third-party control or a simulated form to submit the file.
Enctype = "multipart/form-data" the data format submitted by ajax is not like this.
Directly reading the value of the type = file control cannot upload files.
To upload an ajax file, you must use a third-party control or a simulated form to submit the file.
Can I send a reference example for my reference ??
You cannot do this. I know three methods for uploading files asynchronously. The first HTML5 FILE API method can directly read the FILE content. Second, the Flash plug-in is used for uploading, and all work is done by Flash, such as SwfUpload. The third type is pure JavaScript to complete asynchronous Upload. The third method is described below. To borrow iframe. Reference code:
$ (Document ). on ('change ','. file_upload_control. file_control ', function () {// Obtain the file value var file =$ (this) in the first step ). val (); // verify if (! CheckFile (file) {ClearFileControl ($ (this); // clear the object content return;} // Step 3. if you upload an image, preview PreviewImg ($ file) before local upload; // Step 4, submit the upload $ (this ). parent (). submit (); // submit form // Step 5. if the uploaded file is large (100 MB), check the progress. remember to configure the server to support large files; otherwise, the upload will not succeed! Var file_guid = $ (this ). siblings ('. file_control_guid '). val (); // Step 6: query the file upload progress, which requires the cooperation of the server program. here I will use the PHP code, you need to use the PHP_APC.dll third-party plug-in to help complete setTimeout ('getuploadss SS ("'+ file_guid +'"), 500 );});
Mark !!!!!!