我有一个在其网络界面上具有此javascript功能的设备:
function upload() {
$( "#progress" ).empty();
$( "#uploadresult" ).empty();
// take the file from the input
var file = document.getElementById('files').files[0];
var reader = new FileReader();
reader.readAsBinaryString(file); // alternatively you can use readAsDataURL
reader.onloadend = function(evt)
{
// create XHR instance
xhr = new XMLHttpRequest();
// send the file through POST
xhr.open("POST", 'upload', true);
xhr.setRequestHeader('X-Filename', file.name);
// make sure we have the sendAsBinary method on all browsers
XMLHttpRequest.prototype.mySendAsBinary = function(text){
var data = new ArrayBuffer(text.length);
var ui8a = new Uint8Array(data, 0);
for (var i = 0; i < text.length; i++) ui8a[i] = (text.charCodeAt(i) & 0xff);
if(typeof window.Blob == "function")
{
var blob = new Blob([data]);
}else{
var bb = new (window.MozBlobBuilder || window.WebKitBlobBuilder || window.BlobBuilder)();
bb.append(data);
var blob = bb.getBlob();
}
this.send(blob);
}
// let's track upload progress
var eventSource = xhr.upload || xhr;
eventSource.addEventListener("progress", function(e) {
// get percentage of how much of the current file has been sent
var position = e.position || e.loaded;
var total = e.totalSize || e.total;
var percentage = Math.round((position/total)*100);
// here you should write your own code how you wish to proces this
$( "#progress" ).empty().append('uploaded ' + percentage + '%');
});
// state change observer - we need to know when and if the file was successfully uploaded
xhr.onreadystatechange = function()
{
if(xhr.readyState == 4)
{
if(xhr.status == 200)
{
// process success
$( "#uploadresult" ).empty().append( 'Uploaded Ok');
}else{
// process error
$( "#uploadresult" ).empty().append( 'Uploaded Failed');
}
}
};
// start sending
xhr.mySendAsBinary(evt.target.result);
};
}
在我看来,它似乎正在使用POST上传文件,并且我正尝试使用CURL命令行将文件上传到它,但它一直对我失败,这是我正在使用的命令:curl -F“ [email protected] “ myipaddress /上传
它给了我:FAILED(来自服务器)
怎么了?!
好吧,让我们逐步完成。
该行表示脚本将数据发布到的URL:
xhr.open("POST", 'upload', true);
因此,我们知道您需要命中的终结点是yourdomain.com/upload
我们从这一行看到:
xhr.setRequestHeader('X-Filename', file.name);
该请求正在发送带有文件名的标头,因此我们将确保也包含该标头。
我们还看到它在发送之前将文本编码为二进制,因此我们将仅发送实际文件,而不是尝试首先读取文本或任何内容。
因此,将它们放在一起,您将得到如下所示的内容:
curl -H "X-Filename: yourFileName" -X POST -d @yourFileName http://yourdomain.com/upload
请注意,ipaddress/upload
如果您在本地进行此操作且未设置主机文件,则该url可能会替换为。您可能还需要一个PORT,具体取决于您的配置以及是否在本地进行。看起来像:ipaddress:port/upload
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句