2016-05-23 5 views
0

Zuerst wird der Code:Hochladen von Dateien AJAX JQuery Segel - Paar Fragen

Client-Seite Code:

let formData = new FormData(); 
     formData.append('upload', dataURLtoBlob(dataUrl)); 

     $.ajax({ 
      url: '/Api/File', //Server script to process data 
      type: 'POST', 
      headers: { 
       'authorization': 'Bearer ' + window.localStorage.aurelia_token 
      }, 
      //Ajax events 
      success: (uuid) => { 
       alert(uuid); 
      }, 
      error:() => {alert('Error!');}, 
      // Form data 
      data: formData, 
      //Options to tell jQuery not to process data or worry about content-type. 
      cache: false, 
      contentType: 'multipart/form-data', 
      processData: false 
     }); 

Server-Side-Code:

var file = req.file('upload'); 
    //var extension = file.fd.slice((file.fd.lastIndexOf(".") - 1 >>> 0) + 2); 
    //var newName = uuid.v4(); 
    //file.fd = newName + extension; 

    file.upload({ 
      // don't allow the total upload size to exceed ~4MB 
      maxBytes: 5000000, 
      adapter: require('skipper-s3'), 
      key: sails.config.auth.aws.key, 
      secret: sails.config.auth.aws.secret, 
      bucket: 'bucketName' 
     }, function whenDone(error, uploadedFiles) { 

... 

Ich habe zwei Probleme:

1) Es scheint keine Dateien durchzugehen. Ich bin mir nicht sicher, wo ich die Datei verliere, weil dataURLtoBlob definitiv einen Blob erzeugt ...

2) Wie kann ich den Dateinamen manipulieren, bevor ich die Datei an Amazon sende (Sie können sehen, was ich versucht habe zu tun) in den kommentierten Zeilen)?

Vielen Dank im Voraus.

Antwort

0

HTML-Code

<form name='form1' method='post' enctype='multipart/form-data' id='form-id'> 
    <input type='submit' id='addtag' value='Add' /> 
</form> 

jQuery-Code

$("#form-id").on('submit',(function(e) { 
    e.preventDefault(); 
    $.ajax({ 
     url: "file-to-call.php", 
     type: "POST", 
     data: new FormData(this), 
     cache: false, 
     processData: false, 
     success: function(data) { 
      //handle success 
     } 
    }); 
}));