Request.Files始终为null

编程入门 行业动态 更新时间:2024-10-19 22:28:31
本文介绍了Request.Files始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在编写一个C#ASP.Net MVC应用程序,供客户端将文件发布到其他服务器.我正在使用通用处理程序来处理从客户端到服务器的发布文件.但是在我的处理程序中,System.Web.HttpContext.Current.Request.Files始终为空(0个计数).

I'm writing a C# ASP.Net MVC application for client to post files to other server. I'm using a generic handler to handle posted files from client to server. But in my handler, System.Web.HttpContext.Current.Request.Files always empty (0 count).

表单代码:

@model ITDB102.Models.UploadFileResultsModels @{ Layout = "~/Views/Shared/_Layout.cshtml"; } <div> <h1>Upload File</h1> <form id="file-form" action="/Files/UploadFile" method="post" data-ajax="false" enctype="multipart/form-data"> <div><input type="file" id="FilePath" name="FilePath"/> <button type="submit">Send File</button></div> </form> </div> @section scripts{ <script src="~/Scripts/jquery-1.10.2.js"></script> <script type="text/javascript"> // Variable to store your files var files; var form = document.getElementById('file-form'); // Add events $('input[type=file]').on('change', prepareUpload); // Grab the files and set them to our variable function prepareUpload(event) { files = $('#FilePath').get(0).files; } form.onsubmit = function (event) { uploadFiles(event); } // Catch the form submit and upload the files function uploadFiles(event) { event.stopPropagation(); // Stop stuff happening event.preventDefault(); // Totally stop stuff happening // Create a formdata object and add the files var data = new FormData(); if (files.lenght > 0) { data.append('UploadedFiles', files[0], file[0].name); } //setup request var xhr = new XMLHttpRequest(); //open connection xhr.open('POST', '/Files/UploadFile',false); xhr.setRequestHeader("Content-Type", files.type); //send request xhr.send(data); } </script> }

处理程序:

/// <summary> /// Uploads the file. /// </summary> /// <returns></returns> [HttpPost] public virtual ActionResult UploadFile() { HttpPostedFile myFile = System.Web.HttpContext.Current.Request.Files["UploadedFiles"]; bool isUploaded = false; string message = "File upload failed"; if (myFile != null && myFile.ContentLength != 0) { string pathForSaving = Server.MapPath("~/Uploads"); if (this.CreateFolderIfNeeded(pathForSaving)) { try { myFile.SaveAs(Path.Combine(pathForSaving, myFile.FileName)); isUploaded = true; message = "File uploaded successfully!"; } catch (Exception ex) { message = string.Format("File upload failed: {0}", ex.Message); } } } return Json(new { isUploaded = isUploaded, message = message }, "text/html"); } #region Private Methods /// <summary> /// Creates the folder if needed. /// </summary> /// <param name="path">The path.</param> /// <returns></returns> private bool CreateFolderIfNeeded(string path) { bool result = true; if (!Directory.Exists(path)) { try { Directory.CreateDirectory(path); } catch (Exception) { /*TODO: You must process this exception.*/ result = false; } } return result; } #endregion

请帮助我.谢谢.

推荐答案

最后,我找到了问题.

我的控制器中的代码 var myFile = System.Web.HttpContext.Current.Request.Files ["UploadedFiles"]; 出于某种原因从不工作.我的ajax没错.我在控制器中将代码更改为波纹管,现在可以正常工作了.

The code var myFile = System.Web.HttpContext.Current.Request.Files["UploadedFiles"]; in my controller is never working for some reason. There is nothing wrong with my ajax. I changed my code in the controller as bellow and it's working find now.

[HttpPost] public virtual ActionResult UploadFile() { //var myFile = System.Web.HttpContext.Current.Request.Files["UploadedFiles"]; // bool isUploaded = false; string message = "File upload failed"; for (int i = 0; i < Request.Files.Count; i++ ) { var myFile = Request.Files[i]; if (myFile != null && myFile.ContentLength != 0) { string pathForSaving = Server.MapPath("~/Uploads"); if (this.CreateFolderIfNeeded(pathForSaving)) { try { myFile.SaveAs(Path.Combine(pathForSaving, myFile.FileName)); isUploaded = true; message = "File uploaded successfully!"; } catch (Exception ex) { message = string.Format("File upload failed: {0}", ex.Message); } } } } return Json(new { isUploaded = isUploaded, message = message }, "text/html"); } #endregion #region Private Methods /// <summary> /// Creates the folder if needed. /// </summary> /// <param name="path">The path.</param> /// <returns></returns> private bool CreateFolderIfNeeded(string path) { bool result = true; if (!Directory.Exists(path)) { try { Directory.CreateDirectory(path); } catch (Exception) { /*TODO: You must process this exception.*/ result = false; } } return result; } #endregion }

更多推荐

Request.Files始终为null

本文发布于:2023-11-24 23:00:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627234.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Request   Files   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!