【问题标题】:Uploading files AJAX Jquery to sails - Couple of questions将文件 AJAX Jquery 上传到风帆 - 几个问题
【发布时间】:2016-05-23 00:52:38
【问题描述】:

首先是代码:

客户端代码:

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) 在将文件发送到亚马逊之前如何操作文件名(您可以在注释掉的行中看到我试图做什么)?

提前致谢。

【问题讨论】:

    标签: jquery ajax sails.js


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-02
      • 2014-11-30
      • 1970-01-01
      • 2011-10-11
      • 2011-03-22
      • 2011-07-22
      • 1970-01-01
      相关资源
      最近更新 更多