簡體   English   中英

使用ajax和formData上傳文件

[英]Upload file with ajax and formData

我正在嘗試使用ajax將視頻上傳到Dailymotion API。

在我的腳本中,我有:

                //upload the video and get the url
                var xhr =  new XMLHttpRequest();
                xhr.open('POST', upload_url, true);
                var formData = new FormData(document.getElementById("myForm"));
                xhr.send(formData);

我的腳本可以運行,但是我有問題,如何指定要使用的文件字段?

如果看到var formData = new FormData(document.getElementById("myForm")); myForm是整個表單,如果我的文件輸入具有id="myInput" ,我該如何指定呢?

我不想發送我的所有表格,而只發送一個特定字段。

謝謝 !

這是我想要的:

var file = document.getElementById("myInput").files[0];
var formData = new FormData();
formData.append('file', file);

創建一個空的FormData並手動將文件值添加到其中:

var file = document.getElementById("myInput").value;
var formData = new FormData();
formData.append('file', file);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM