【问题标题】:In REST API how to attach files to the body of form data in C#在 REST API 中,如何将文件附加到 C# 中的表单数据主体
【发布时间】:2022-01-02 18:49:32
【问题描述】:

我正在使用 Postman Rest API 调用将文件上传到 Azure Blob。我想了解如何使用 C# 代码从前端将文件附加到表单数据主体,以便它返回带有一些 ID 和文件类型的结果。

【问题讨论】:

  • 你没有说你是否使用 MVC C#,但模式类似于this
  • 谢谢!我正在尝试使用 MVC C# 从前端上传图像文件,我想知道如何将文件上传到表单数据的主体。如果有人能对此提供支持,将不胜感激。
  • 在这种情况下,我链接到的答案有你需要使用的确切方法
  • 这能回答你的问题吗? jQuery ajax upload file in asp.net mvc

标签: javascript c# jquery controller image-upload


【解决方案1】:

我认为这是一个duplicated 问题

从原帖中添加答案

document.getElementById('uploader').onsubmit = function () {
    var formdata = new FormData(); //FormData object
    var fileInput = document.getElementById('fileInput');
    //Iterating through each files selected in fileInput
    for (i = 0; i < fileInput.files.length; i++) {
        //Appending each file to FormData object
        formdata.append(fileInput.files[i].name, fileInput.files[i]);
    }
    //Creating an XMLHttpRequest and sending
    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/Home/Upload');
    xhr.send(formdata);
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            alert(xhr.responseText);
        }
    }
    return false;
}   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    相关资源
    最近更新 更多