asp.net mvc 3 razor文件上传
哪个是使用asp.net mvc3 razor上传单个文件并使用jqueryvalidation的最佳方法。
我只需要用户上传jpg,png少于5 mb。
谢谢
您需要使用javascriptvalidation,这是一个示例
function onSelect(e) { if (e.files[0].size > 256000) { alert('The file size is too large for upload'); e.preventDefault(); return false; } // Array with information about the uploaded files var files = e.files; var ext = $('#logo').val().split('.').pop().toLowerCase(); if ($.inArray(ext, ['gif', 'jpeg', 'jpg', 'png', 'tif', 'pdf']) == -1) { alert('This type of file is restricted from being uploaded due to security reasons'); e.preventDefault(); return false; } return true; }
这表示文件必须不大于256K并且只允许gif,jpg,jpeg,tif,png和pdf。 只需更改256000到5000000以及您的特定文件类型即可
我在使用Telerik上传控件的剃刀视图中在MVC 3中使用它。 您也可以使用标准上传输入,只需在选择时或提交前触发此事件
除了jQueryvalidation(非常好的Acid的答案)你还应该进行服务器validation。 这是一些简单的例子:
视图:
@if (TempData["imageUploadFailure"] != null) { @* Here some jQuery popup for example *@ } @using (Html.BeginForm("ImageUpload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) {
}
控制器:
public ActionResult ImageUpload() { return View(); } [HttpPost] public ActionResult ImageUpload(HttpPostedFileBase image) { var result = ImageUtility.SaveImage("/Content/Images/", 1000000, "jpg,png", image, HttpContext.Server); if (!result.Success) { var builder = new StringBuilder(); result.Errors.ForEach(e => builder.AppendLine(e)); TempData.Add("imageUploadFailure", builder.ToString()); } return RedirectToAction("ImageUpload"); }
ImageUtility助手类:
public static class ImageUtility { public static SaveImageResult SaveImage(string path, int maxSize, string allowedExtensions, HttpPostedFileBase image, HttpServerUtilityBase server) { var result = new SaveImageResult { Success = false }; if (image == null || image.ContentLength == 0) { result.Errors.Add("There was problem with sending image."); return result; } // Check image size if (image.ContentLength > maxSize) result.Errors.Add("Image is too big."); // Check image extension var extension = Path.GetExtension(image.FileName).Substring(1).ToLower(); if (!allowedExtensions.Contains(extension)) result.Errors.Add(string.Format("'{0}' format is not allowed.", extension)); // If there are no errors save image if (!result.Errors.Any()) { // Generate unique name for safety reasons var newName = Guid.NewGuid().ToString("N") + "." + extension; var serverPath = server.MapPath("~" + path + newName); image.SaveAs(serverPath); result.Success = true; } return result; } } public class SaveImageResult { public bool Success { get; set; } public List Errors { get; set; } public SaveImageResult() { Errors = new List (); } }
您还可以修改响应格式,不同的文件重命名或添加多个文件处理function等。
这只是指定要接受的文件类型:MSvisualstudio2010。
在您的视图(.cshtml)中:
ATTACHMENT:
只需指定所需的格式即可。
- 将ajax $ get()放入javascript变量中
- Tag-it onlyAvalaibleTags选项不起作用
- 如何通过Jquery调用MVC FileContentResult并让它提示用户保存它的返回值?
- MVC3和JSON.stringify()ModelBinding返回null模型
- 将javascript全局变量设置为JSONresult?
- 如何在 _Layout.cshtml中使用properties.settings.default
- 使用Twitter Bootstrap在ASP.NET MVC中调用模式对话框的最佳方法是什么?
- 作为就业申请的一部分,在c#MVC3(非托管)中的应用程序书面签名
- 如何在AJAX中处理JSON输出?