简体   繁体   English

将文件上传的多个参数发布到 web api

[英]Post more than one parameter for file upload to the web api

I have this html form which let user upload a file and also another extra input type for extra info about this upload.我有这个 html 表单,它让用户上传一个文件,还有另一个额外的输入类型,用于获取有关此上传的额外信息。

<form method="post" enctype="multipart/form-data">
       <input id="files" name="files" type="file" size="1" />           
       <input type="text" id="tags">           
       <button type="submit" id="btnSave" onchange="uploadFiles();">Upload and Save</button>        
</form>

and this is my javascript这是我的 javascript

function uploadFiles() {
    var input = document.getElementById('files');
    var files = input.files;
    var formData = new FormData();

    for (var i = 0; i != files.length; i++) {
        formData.append("files", files[i]);
    }

    $.ajax(
        {
            url: "https://localhost:xxx/api/file/upload",
            data: formData,
            processData: false,
            contentType: false,
            type: "POST",
            success: function () {
                alert("Files Uploaded!");

            }
        }
    );
}

My question is how do I add others parameters (ie tags here) to post to web api and at the same time upload the file?我的问题是如何添加其他参数(即此处的标签)以发布到 web api 并同时上传文件? Post the request at complex type?以复杂类型发布请求? Any example for complex type which include file upload?包含文件上传的复杂类型的任何示例?

You just can append new properties there.您只需在那里附加新属性即可。 formdata append will help you to add string and file formdata append 将帮助您添加字符串和文件

formData.append("tags",document.getelementbyid('tags').value);

this is some source to read about FormData https://developer.mozilla.org/en-US/docs/Web/API/FormData这是阅读有关 FormData https://developer.mozilla.org/en-US/docs/Web/API/FormData 的一些来源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM