0
結合使用幾個不同的腳本我試圖創建一個工具,允許用戶將視頻從本地機器上傳到S3存儲桶。它在沒有任何空格的文件上出色地工作,但如果有任何空格,則完全窒息(上傳死亡,並且我得到XHR錯誤)。PHP/jQuery S3上傳不適用於帶空格的文件
上傳形式:
<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> </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>
這裏的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); } }
因此,用戶選擇視頻文件,點擊提交,並handleFileSelect被觸發。最好我想在上傳之前重命名文件,但我甚至無法做到這一點。從規格看來,這可能是不可能的。那麼,到底發生了什麼?認爲我無法上傳一個有空格的文件是瘋了,所以我必須犯錯誤,對吧?