【问题标题】:I need to Convet Dart Function to Vue js axios.post()我需要将 Dart 函数转换为 Vue js axios.post()
【发布时间】:2021-08-25 10:06:41
【问题描述】:

这是我将选定图像上传到节点服务器的 dart 函数,我需要使用 Vue js 来执行此操作。

 var stream = new http.ByteStream(DelegatingStream.typed(file.openRead()));
    var length = await file.length();
    var uri = Uri.parse(_url);
    var request = new http.MultipartRequest("POST", uri);
    var multipartFile = new http.MultipartFile('img', stream, length,
        filename: Path.basename(file.path));
    request.files.add(multipartFile);
    var response = await request.send();

【问题讨论】:

    标签: javascript node.js vue.js dart multipartform-data


    【解决方案1】:
    <input id="file-upload" type="file" />
    

    在 Vue Methods 中,您创建一个发送请求的异步函数。

    let file = document.querySelector("#file-upload").files[0];
    const formData = new FormData();
    formData.append("binFile", file);
    
    try{
     let res = await axios.post('<enpoint upload url>', formData, {
       headers: { "Content-Type": "multipart/form-data" }
     });
    }
    catch(e){
    //in case await fails(4xx,5xx error)
    }
    
    

    【讨论】:

    • 在 MultipartRequest 有自定义对象创建的问题中。 ``` http.MultipartFile('img', stream, length, filename: Path.basename(file.path));``` 这样行吗?
    • 我也有同样的问题。所以需要创建该对象自定义或 [file] 对象默认采用这种格式@alexkarpen
    猜你喜欢
    • 1970-01-01
    • 2021-11-06
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-12
    • 1970-01-01
    • 2020-10-02
    相关资源
    最近更新 更多