簡體   English   中英

嘗試通過http上傳文件時發送目標文件而不是文件信息

[英]object file being sent instead of file information while trying to upload file through http

試圖通過Angular $ http上傳文件。 我正在設置Content-Type並使用FormData對象將其包裝起來,如大多數地方所建議的那樣。 但是,瀏覽器在http請求的請求有效負載中發送[對象數據]。

服務電話:

let fd = new FormData();

            fd.append('file', file);
            return $q((resolve, reject) => {
                $http.post(url, fd, {
                    headers: {
                        'Content-Type': undefined,
                        'X-Token': undefined
                    }
                }).then(
                        success => resolve(success.data),
                        error => reject(error)
                    );
            });

關於為什么瀏覽器無法對此文件進行編碼的想法? 請注意,到目前為止,我已嘗試過docx,zip,mp4文件格式。 它們都產生相同的結果。

請求負載:

------WebKitFormBoundaryqfOlKmKqvArA9pz6
Content-Disposition: form-data; name="file"

[object File]
------WebKitFormBoundaryqfOlKmKqvArA9pz6--

標記使用:

<input type="file" id="file-tutorial-{{$index}}-{{language.iso2}}" name="file-tutorial-{{$index}}-{{language.iso2}}"
                                     onchange="angular.element(this).scope().ctrl.addFile(this);">

Ctrl.addFile:

addFile(element) {
    service.uploadFile(element.files[0]);
    this.$scope.$digest();
}

element.files [0]或文件對象:

{lastModified:1496720186000
 lastModifiedDate:Mon Jun 05 2017 23:36:26 GMT-0400 (EDT)
 name:"Adil Sulaiman Resume.docx"
 size:25456
 type:"application/vnd.openxmlformats-
 officedocument.wordprocessingml.document"
 webkitRelativePath:""}

似乎您缺少angular.identity ,這會阻止Angular對您要發送的文件執行任何操作,例如序列化它。 請在此處查看說明

您正在使用本機onchange事件,該事件超出了角度范圍。 查看本文 ,了解如何獲取對文件對象的訪問權限。

可以在這里找到工作演示

暫無
暫無

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

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