[英]Multiple File Upload jquery ajax with extension
I have problem in uploading multiple file upload using jquery/ajax. 我在使用jquery / ajax上传多个文件时遇到问题。
my problem is when i try to submit a file with png or jpg file it didnt even submit and it will alert Please Upload file even though i have put a file. 我的问题是,当我尝试提交带有png或jpg文件的文件时,它甚至没有提交,即使我已经放置了文件,它也会警告请上传文件。
what should i do in order to go in my else statement. 我应该怎么做才能进入我的else语句。 coz i tried to remove my condition i just put the ajax and it works and put to my database. 因为我试图消除我的病情,所以我只是把ajax放到了我的数据库中。 but when i put a condition with a proper file extension even if i have put jpg or png file still it will alert please upload file. 但是,即使我放置了jpg或png文件,但当我放置带有适当文件扩展名的条件时,它也会发出警报,请上传文件。
here is my CODE 这是我的密码
HTML HTML
<!-- MULTIPLE UPLOAD FORM -->
<h1>Multiple File Upload</h1>
<form id="multiple" method="POST" enctype="multipart/form-data">
<input type="file" id="file" name="file[]" multiple />
<input type="submit" id="multBtn" value="Upload File">
</br>
</form>
Jquery jQuery的
$("#multBtn").on('click',function(e){
e.preventDefault();
var imgData = document.getElementsByName('file')[0];
var imageUploadPath = imgData.value;
if(imageUploadPath == ''){ // user did not upload anything
alert('Please Upload File');
}
else{
//user uploaded a file
var Extension = imageUploadPath.substring(imageUploadPath.lastIndexOf('.')+1).toLowerCase();
if(Extension == "png" || Extension == "Jpeg" || Extension == "jpg"){
$.ajax({
url: '/uploadMultipleImg',
type: 'POST',
data: new FormData($(this).parents('form')[0]),
cache: false,
contentType: false,
processData: false,
success: function(data){
},
});
return false;
} }
To check if the file is selected use: 要检查是否选择了文件,请使用:
var fileUpload = $("#file").get(0);
var files = fileUpload.files;
Then loop through the files like this: 然后像这样遍历文件:
for (var i = 0; i < files.length; i++) {
//code
}
Change your file input name and make it same as id. 更改文件输入名称,并使其与id相同。
<input type="file" id="file" name="file" multiple />
You can see this tutorial on file upload with ajax which will help. 你可以看到这对文件上传教程使用Ajax,这将有助于。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.