将文件AJAX Jquery上传到风帆 - 几个问题

时间:2016-05-23 00:52:39

标签: jquery ajax sails.js

首先是代码:

客户端代码:

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

服务器端代码:

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

 ...

我有两个问题:

1)似乎没有文件通过。我不知道我丢失文件的位置因为dataURLtoBlob肯定会产生一个blob ...

2)如何在将文件发送到亚马逊之前操作文件名(你可以在注释掉的行中看到我试图做的事情)?

提前致谢。

1 个答案:

答案 0 :(得分:0)

  

HTML代码

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

jQuery代码

$("#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
        }
   });
}));