Upload files with HTML/PHP/AJAX and especially SweetAlert2/jquery_file_upload
我想使用PHP和js库SweetAlert2将文件从HTML上传到我的服务器。
如果我这样做就行
1 2 3 | swal({ html: '<form action="script/php/upload.php" method="post" enctype="multipart/form-data"><input name=\"upload[]\" id="fileToUpload" accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" multiple></input></form>' }) |
但是我没有完全使用" SweetAlert2"的功能,因此我尝试使用此JAVASCRIPT / AJAX
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | swal({ input: 'file', inputAttributes: { name:"upload[]", id:"fileToUpload", accept:"application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", multiple:"multiple" }, showCloseButton: true, showCancelButton: true }).then(function() { var formData = new FormData(); formData.append('fileToUpload', $('#fileToUpload')[0]); $.ajax({type:"POST",url:"script\\\\php\\\\upload.php",data: formData,processData: false,contentType: false,headers:{"Content-Type":"multipart/form-data"},async:false}); }) |
但这行不通...
我这样的PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 | $total = count($_FILES['upload']['name']); echo($total); for($i=0; $i<$total; $i++) { $tmpFilePath = $_FILES['upload']['tmp_name'][$i]; if ($tmpFilePath !=""){ $newFilePath ="d:/web/site_sig_cclo/site_3_administration/script/python/" . $_FILES['upload']['name'][$i]; if(move_uploaded_file($tmpFilePath, $newFilePath)) { } } } |
并且
------编辑2016 10 05 ------
我的PHP使用此Post值启动
------编辑2016 10 06 ------
如果在
如果我在FormData上做同样的事情,它似乎是空的:
------编辑2016年10月12日----
我尝试使用脚本
我将输入加载到小号中
1 2 3 4 | swal({ title:"Input title", html: '<input id="fileupload" type="file" name="files[]" multiple>' }); |
在使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $.getScript("script/jquery_file_upload/js/vendor/jquery.ui.widget.js"); $.getScript("script/jquery_file_upload/js/jquery.iframe-transport.js"); $.getScript("script/jquery_file_upload/js/jquery.fileupload.js", function(){ $("#fileupload").fileupload({ url:"script/python", dataType: 'json', add: function (e, data) { data.context = $('#fileuploadname').text('Uploading...').appendTo(document.body); data.submit(); }, done: function (e, data) { data.context.text('Upload finished.'); } }); }); |
如果我对
解决方法是打开swal功能
1 2 3 4 5 6 | swal({ title:"Input title", html: '<input id="fileupload" type="file" name="files[]" multiple>' }).then(function() { (...) } |
并且在加载脚本之后,使用带有jquery功能的脚本以与swal相同的级别进行文件上传。 在此脚本中,有必要调用php代码以上传文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $.getScript("script/jquery_file_upload/js/vendor/jquery.ui.widget.js"); $.getScript("script/jquery_file_upload/js/jquery.iframe-transport.js"); $.getScript("script/jquery_file_upload/js/jquery.fileupload.js", function(){ $("#fileupload").fileupload({ url:"script/php/myuploadphp.php", dataType: 'json', add: function (e, data) { data.context = $('#fileuploadname').text('Uploading...').appendTo(document.body); data.submit(); }, done: function (e, data) { data.context.text('Upload finished.'); } }); }); |
然后用PHP编写
1 2 3 4 5 | $tmpFilePath = $_FILES['files']['tmp_name'][0]; if ($tmpFilePath !=""){ $newFilePath ="destination_folder" . $_FILES['files']['name'][0]; } |
就是这样!