我正在开发一个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,我们只能在文件已经保存到硬盘驱动器后才能访问非文件参数.
我们可以找到的唯一解决方法是发送IEnumerable< FileModel>参数在URL中.但是鉴于URL的最大长度有限,这不是一个可靠的方法.
问题是:有没有办法提交IEnumerable< FileModel> 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' }); });
非常感谢你!
解决方法
如果你想看到代码,这对我们的例子是有效的,我相信它很简单,以适应任何工作情况:
在客户端,我们有以下形式(是的,这个实现是为了演示目的,只支持发送一个文件…也是,输入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/",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请求一次提交文件和其他参数到服务器!