2016-10-05 1 views
1

Ich möchte Datei von HTML auf meinen Server mit PHP und js Bibliothek SweetAlert2 hochladen.Hochladen von Dateien mit HTML/PHP/AJAX und vor allem SweetAlert2/jquery_file_upload

Es funktioniert, wenn ich

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>' 
}) 

Aber ich weiß nicht vollständig Macht der "SweetAlert2" dass

tun verwenden, damit ich dieses JAVASCRIPT versuchen/AJAX

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}); 
}) 

Aber es doesn‘Arbeit ...

Meine PHP so

$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)) { 
    } 
    } 
} 

Und echo immer wieder zurückkehren 0

------ EDIT 2016 10 05 ------

Meine PHP ist mit diesem Beitrag Wert gestartet

enter image description here

------ EDITIEREN 2016 10 06 ------

Wenn ich console.log auf meine Eingabedatei nach dem .then(function(file) { verwende ich habe:

enter image description here

Und wenn ich auf meinem Formdata das gleiche tun, so scheint es, leer zu sein:

enter image description here

------ EDITIEREN 2016 10 12 ------

Ich versuche eine Technik w fileupload

$.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.'); 
     } 
    }); 
}); 

it dem Skript jquery_file_upload

lade ich Eingang in meinem SwaI

swal({ 
    title: "Input title", 
    html: '<input id="fileupload" type="file" name="files[]" multiple>' 
}); 

Und nachdem ich meine Skripte mit Jquery Funktion laden Wenn ich console.log auf data Werte tun Ich kann sehen, alles funktioniert, aber auf meinem Server habe ich nichts ...

enter image description here

+1

Ich denke, das ist falsch 'script \\ php \\ upload.php' Sie müssen nicht die Schrägstriche zu entkommen' script/php/upload.php' sollte funktionieren. – Franco

+0

Vielen Dank für Ihren Kommentar Franco, aber es funktioniert nicht besser –

+0

Haben Sie irgendwelche nachvollziehbare Fehler, wenn Sie es verwenden? – Franco

Antwort

0

Die Lösung ist SwaI Funktion

swal({ 
    title: "Input title", 
    html: '<input id="fileupload" type="file" name="files[]" multiple>' 
}).then(function() { 
    (...) 
} 

Und nach Ladeskripts mit Jquery Funktion für Fileupload auf dem gleichen Niveau wie SwaI zu öffnen.In diesem Skript schreibt

$.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.'); 
     } 
    }); 
}); 

Und in PHP zu nennen, es ist notwendig, den PHP-Code zum Hochladen von Dateien, dass

$tmpFilePath = $_FILES['files']['tmp_name'][0]; 

     if ($tmpFilePath != ""){ 
     $newFilePath = "destination_folder" . $_FILES['files']['name'][0]; 
     } 

und das ist Arbeit!

Verwandte Themen