简体   繁体   English

PHP/jQuery S3 上传不适用于带空格的文件

[英]PHP/jQuery S3 upload doesn't work on files with spaces

Using a combination of a few different scripts I'm trying to create a tool that will allow users to upload videos from their local machine to a S3 bucket.使用几个不同脚本的组合,我试图创建一个工具,允许用户将视频从他们的本地机器上传到 S3 存储桶。 It works brilliantly on files without any spaces, but completely chokes if there are any spaces (the upload dies and I get a XHR error).它在没有任何空格的文件上表现出色,但如果有任何空格则完全窒息(上传失败并且我收到 XHR 错误)。

Upload form:上传表格:

<table>
    <tr>
        <td>File:</td>
        <td><input type="file" id="files" name="files[]" multiple /></td>
    </tr>
    <tr>
        <td>Title:</td>
        <td><input type="text" id="title" name="title" size="50" /></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
        <td><input type="submit" id="submit" name="submit" value="Start Encoding" /></td>
    </tr>
</table>

<script type="text/javascript">
document.getElementById('submit').addEventListener('click', handleFileSelect, false);
setProgress(0, 'Waiting for upload.');
</script>

Here's the javascript:这是javascript:

function handleFileSelect() {
        setProgress(0, 'Upload started...');

        var files = document.getElementById('files').files;
        var title = document.getElementById('title').value;

        var pieces = files[0].name.split(".");
        var fext = "-original." + pieces[pieces.length - 1]; //fext = "-original.jpg"
        var currdatetime = new Date().getTime(); 
        var dockey = "FINVID" + currdatetime; //dockey = "FINVID20130523123546

        files[0].name = dockey;

        $.get("updatetxtfile.php", { vidid: dockey, vidtitle: title } );
        uploadFile(files[0],fext);
    }


    function uploadFile(file,fext){
      executeOnSignedUrl(file, function(signedURL) 
      {
        uploadToS3(file, signedURL, fext);
      },fext);
    }


    function uploadToS3(file, url, fext){
        var xhr = createCORSRequest('PUT', url);
        if (!xhr) {
            setProgress(0, 'CORS not supported');
        }
        else
      {
        xhr.onload = function() 
        {
          if(xhr.status == 200)
          {
            setProgress(100, 'Upload completed');
                    triggerEncoding(file, fext);
          }
          else
          {
            setProgress(0, 'Upload error: ' + xhr.status);
          }
        };

        xhr.onerror = function() 
        {
          setProgress(0, 'XHR error.');
        };

        xhr.upload.onprogress = function(e) 
        {
          if (e.lengthComputable) 
          {
            var percentLoaded = Math.round((e.loaded / e.total) * 100);
            setProgress(percentLoaded, percentLoaded == 100 ? 'Finalizing...' : 'Uploading...');
          }
        };

        xhr.setRequestHeader('Content-Type', file.type);
        xhr.setRequestHeader('x-amz-acl', 'public-read');

        xhr.send(file);
      }
    }

So, a user selects a video file, hits submit, and handleFileSelect gets triggered.因此,用户选择一个视频文件,点击提交,然后 handleFileSelect 被触发。 Optimally I'd like to rename the file before upload, but I can't even do that.最好我想在上传之前重命名文件,但我什至不能这样做。 It looks like from the specs that might not be possible.从规格来看,这似乎是不可能的。 So what the heck is going on?那么到底发生了什么? It's insane to think that I can't upload a file with a space in it, so I must be making a mistake, right?认为我不能上传带有空格的文件是很疯狂的,所以我一定是犯了一个错误,对吧?

Check this out.看看这个

It looks like you can easily manage the key value (either statically or dinamically)看起来您可以轻松管理键值(静态或动态)

<input type="hidden" name="key" value="uploads/${filename}">

and the callback action和回调动作

<input type="hidden" name="success_action_redirect" value="http://localhost/">

without messing up much with the manual upload.不会对手动上传造成太大影响。

You can always make it ajax if you want, but make sure you're using all the very useful parameters they accept.如果需要,您可以随时使用 ajax,但请确保使用它们接受的所有非常有用的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM