【问题标题】:How to upload files to a Web API server and send parameters along to the action?如何将文件上传到 Web API 服务器并将参数发送到操作?
【发布时间】:2015-03-09 05:16:13
【问题描述】:

美好的一天!

我正在开发一个 ASP.NET Web API 2 项目。在某个时候,需要上传一些文件。这些文件需要链接到某个 FileModel(我们自己的类)。因此,客户端需要发送 IEnumerable 作为参数和文件作为内容。因为它是一个 RESTful API,所以两者必须在同一个请求中发送。

我们能想到的最好的方法是跟随控制器动作:

public async Task<HttpResponseMessage> Add([FromUri] IEnumerable<FileModel> fileModels)
{
   // REQUEST INTEGRITY TESTING

   var streamProvider = new CustomMultipartFormDataStreamProvider(fileSavePath, fileModels);
   // Read the MIME multipart content using the stream provider we just created.
   var work = await Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(async t =>
        {
            // SOME WORK DONE AFTER SAVING THE FILES TO THE HARD DRIVE
        }

}

问题如下:上传的文件带有“multipart/form-data”Content-Type 标头。 在服务器端操作文件之前,我们需要知道 FileModels 的内容。如果我们使用MultipartFormDataStreamProvider,我们只能在文件已经保存到硬盘后才能访问非文件参数。

我们能找到的唯一解决方法是在 URL 中发送 IEnumerable 参数。但鉴于 URL 的最大长度有限,这不是一种可靠的方法。

问题是:有没有办法同时提交 IEnumerable fileModels 参数和文件在请求正文中 并获得对 fileModels 的访问权限访问文件之前的参数?我们还希望能够使用 HttpContext.Current.Request.Files.Count;

我们目前用于文件上传的jQuery是这样的(为了早期测试,它只支持一个文件上传):

$('#Upload').click(function(e) {
            e.preventDefault();

            var headers = new Array();
            headers["SessionId"] = sessionId;

            var files = $('#fileInput').get(0).files;
            var formData = new FormData();
            formData.append("files", files[0]);

            var fileModel = $('#fileSubmission').serialize();

            $.ajax({
                url: "api/Submissions/Add/?" + fileModel,
                headers: headers,
                type: 'POST',
                data: formData,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json'
            });
        });

非常感谢!

【问题讨论】:

  • 您是否考虑过将整个数据以 JSON 格式发送并简单地将其反序列化为包含 Stream 和元数据参数的类?
  • 是的,但文件最终总是为空。
  • 您是否尝试过使用 MultipartFormDataStreamProvider。看看这个样本damienbod.wordpress.com/2014/03/28/…

标签: c# jquery file-upload asp.net-web-api2 multipartform-data


【解决方案1】:

很抱歉回答迟了,但我们解决了问题(我忘了我没有在这里上传答案)。基本上我们所做的是在一个临时位置调用 ReadAsMultiPartAsync 方法,然后从请求中提取其他参数。之后,我们验证了输入并将文件从临时位置移动到永久位置。

如果您想查看代码,这对我们的特定示例有效,我相信它可以非常简单地适应任何工作案例场景:

在客户端,我们有以下表单(是的,这个实现是为了演示目的,只支持发送一个文件......而且,输入 type="file" 字段确实在表单之外;fileId在我们的例子中,文本输入是手动完成的,仅用于测试目的)

<input type="file" name="data" id="fileInput" multiple="multiple" />

<form id="fileSubmission">            
    <input type="text" width="10" onchange="getFileDetails()" autocomplete="off" placeholder="FileId" name="files[0].Id" id="fileId" /> 
    <input type="hidden" name="files[0].FileName" id="FileName"/>
    <input type="hidden" name="files[0].Extension" id="Extension"/>
    <input type="hidden" name="files[0].EntityId" id="EntityId"/>
    <br /><br />
    <input type="submit" id="Upload" value="Upload" />
</form>

其中 getFileDetails() 填充其他输入字段。此外,表单正在使用以下 jQuery/Javascript 发送到服务器:

$('#Upload').click(function(e) {
            e.preventDefault();

            var courseId = $('#courseId').val();
            var fileId = $('#fileId').val();
            if (!courseId || !fileId) {
                return;
            }

            var headers = new Array();
            headers["SessionId"] = sessionId;
            headers["contentType"] = "application/json; charset=UTF-8";

            var formData = new FormData();
            var opmlFile = $('#fileInput').get(0).files;

            // this is like the model we're expecting on the server
            var files = [];
            files.push({ 'Id': $('#fileId').val(), 'OriginalFileName': opmlFile[0].name, 'FileName': $('#FileName').val(), 'Extension': $('#Extension').val(), 'EntityId': $('#EntityId').val() });

            formData.append("fileModels", JSON.stringify(files));
            formData.append("File_0", opmlFile[0]);


            $.ajax({
                url: "api/Courses/" + courseId + "/Submissions/Add/",
                headers: headers,
                type: 'POST',
                data: formData,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json'
            });
        });

在服务器端,我们有以下内容:

// POST: api/Courses/{courseId}/Submissions/Add
[HttpPost]
[ValidateModelState]
[ValidateMimeMultipartContent]
[PermissionsAuthorize(CoursePermissions.CanCreateSubmissions)]
public async Task<HttpResponseMessage> Add(int courseId)
    {
        // the same as in the jQuery part
        const string paramName = "fileModels";

        // Put the files in a temporary location
        // this way we call ReadAsMultiPartAsync and we get access to the other data submitted
        var tempPath = HttpContext.Current.Server.MapPath("~/App_Data/Temp/" + Guid.NewGuid());
        Directory.CreateDirectory(tempPath);

        var streamProvider = new MultipartFormDataStreamProvider(tempPath);
        var readResult = await Request.Content.ReadAsMultipartAsync(streamProvider);

        if (readResult.FormData[paramName] == null)
        {
            // We don't have the FileModels ... delete the TempFiles and return BadRequest
            Directory.Delete(tempPath, true);
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }

        // The files have been successfully saved in a TempLocation and the FileModels are not null
        // Validate that everything else is fine with this command
        var fileModels = JsonConvert.DeserializeObject<IEnumerable<FileModelExtension>>(readResult.FormData[paramName]).ToList();

        // AT THIS POINT, ON THE SERVER, WE HAVE ALL THE FILE MODELS 
        // AND ALL THE FILES ARE SAVED IN A TEMPORARY LOCATION

        // NEXT STEPS ARE VALIDATION OF THE INPUT AND THEN 
        // MOVING THE FILE FROM THE TEMP TO THE PERMANENT LOCATION

        // YOU CAN ACCESS THE INFO ABOUT THE FILES LIKE THIS:
        foreach (var tempFile in readResult.FileData)
            {
                var originalFileName = tempFile.Headers.ContentDisposition.FileName.Replace("\"", string.Empty);

                var localTempPath = tempFile.LocalFileName;
            }

    }

我希望这将帮助那些尝试使用 Post 请求同时向服务器提交文件和其他参数的人! :)

注意:服务器上使用的一些属性是自定义的。 PermissionAuthorize、ValidateModelState 和 ValidateMimeMultiPartContent 是我们使用的自定义过滤器。后两者的实现受到http://benfoster.io/blog/automatic-modelstate-validation-in-aspnet-mvc的启发

multipartcontent 属性只是对 actionContext.Request.Content.IsMimeMultipartContent() 进行检查,如下所示:

public class ValidateMimeMultipartContent : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if (!actionContext.Request.Content.IsMimeMultipartContent())
        {
            actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.UnsupportedMediaType, Messages.UnsupportedMediaType);
        }
    }
}

【讨论】:

    猜你喜欢
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 2015-10-02
    相关资源
    最近更新 更多