【问题标题】:How to send a url in form-data as a file using js or axios如何使用 js 或 axios 将表单数据中的 url 作为文件发送
【发布时间】:2021-03-09 18:32:00
【问题描述】:

我正在使用 vuejs 和 axios 将 formdata 发送到 somelink。文件未发送并且出现错误

The parameter 'file' had the following problems: file transferred without multipart should be base64 encoded

我确实更改了 data.append('file', url) 并添加了另一个参数,例如 data.append('file', url,{type:'pdf'}) 但后来我收到一条错误消息第二个参数不是 blob。 我知道这个问题是因为我在文件中使用了一个 url 而这我无法更改,因为发送文件的 api 文档需要发送带有文件的表单数据,所以我试图用一个实时 url 替换文件

var data = new FormData();
        var url= 'https://storage.cloudconvert.com/xxxxx.pdf';
        data.append('name','file')
        data.append('filename', 'amjad');
        data.append('file', url);
        data.append('saved', 'true');
        data.append('type', 'pdf');
        axios.post('myapiurl',data, {
            headers: {
                'Content-Type': 'multipart/form-data'
            },

        }).then(res => {
            console.log(res);
        })

【问题讨论】:

    标签: javascript vue.js axios blob


    【解决方案1】:

    尝试打开文件,然后对其进行编码.. 像这样..

     function getPdf(){
            // read text from URL location
            var request = new XMLHttpRequest();
            request.open('GET', 'https://storage.cloudconvert.com/xxxxx.pdf', true);
            request.send(null);
            request.onreadystatechange = function () {
                if (request.readyState === 4 && request.status === 200) {
                    var type = request.getResponseHeader('Content-Type');
                    if (type.indexOf("pdf") !== 1) {
                        return request.responseText;
                    }
                }
            }
        }
    // Encode the String
        var pdf = btoa(getPdf()); 
        
        
        var data = new FormData();
               
                data.append('name','file')
                data.append('filename', 'amjad');
                data.append('file', pdf);
                data.append('saved', 'true');
                data.append('type', 'pdf');
                axios.post('myapiurl',data, {
                    headers: {
                        'Content-Type': 'multipart/form-data'
                    },
        
                }).then(res => {
                    console.log(res);
                })
    

    【讨论】:

      猜你喜欢
      • 2020-11-14
      • 1970-01-01
      • 2018-12-24
      • 2021-02-09
      • 2015-07-10
      • 2019-04-23
      • 1970-01-01
      • 2020-11-21
      • 2022-08-08
      相关资源
      最近更新 更多