2017年12月6日 星期三

ajax檔案上傳

之前有做過圖片上傳,把遇到的事情記下來

用post方式傳檔案
<input type="file" name="myfile">

我後端用PHP抓不到
$_FILES["myfile"]["name"]

換這樣
<input type="file" id="myfile">
就抓的到了
居然是吃ID(是我見鬼嗎),這樣對嗎?



換ajax傳檔案

<input type="file" id="myfile">

我後端用PHP抓不到
$_FILES["myfile"]["name"]

換這樣
<input type="file" name="myfile">
換這樣就抓的到了
變正常吃NAME了
$("form").submit(function(e) {
e.preventDefault();
var formData = new FormData(this);

$.ajax({
url: '/123.php',
type: 'POST',
data: formData,
success: function (data) {
},
cache: false,
contentType: false,
processData: false
});
});

這樣欺負我QQajax傳檔案用formData來做
要注意:
through FormData object, but unfortunately it is not supported by all/old browsers.FormData support starts from following desktop browsers versions. IE 10+, Firefox 4.0+, Chrome 7+, Safari 5+, Opera 12+


附上ajax傳檔的連結:
http://www.jianshu.com/p/46e6e03a0d53
https://stackoverflow.com/questions/2320069/jquery-ajax-file-upload
檔案加資料一起傳
https://stackoverflow.com/questions/10899384/uploading-both-data-and-files-in-one-form-using-ajax



沒有留言:

張貼留言