验证之前上传文件大小 [英] Validate File size before upload

查看:186
本文介绍了验证之前上传文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要验证将被上传到服务器上的文件。验证必须在上传之前完成。即,验证完成了客户端。这个任务应该在ASP.NET MVC3网页来完成。还应该与所有的浏览器。 IE9,8,7 / FF /铬。我才知道,IE浏览器不具备的FileReader API。

I need to validate the file which is to be uploaded to the server. The validation must be done before uploading it. i.e., validation completed at client side. This task should be accomplished in ASP.NET MVC3 Webpage. It should also work with all browsers. IE9,8,7/FF/Chrome. I came to know that IE doesn't have FileReader API.

我的问题是,如何在MVC3网页上载前验证文件的大小。

My Question is, How to Validate file size before Uploading in a MVC3 Webpage.

推荐答案

的.Net MVC解决方案:

我使用 HttpPostedFileBase

在你的查看>共享文件夹,创建一个名为EditorTemplates新的文件夹,并使用此:

In your Views > Shared Folder, create a new folder called "EditorTemplates" and use this:

@model HttpPostedFileBase

@Html.TextBox("", null, new { type = "file" })

然后我从控制器通过这个 HttpPostedFileBase 对象,做以下的方法:

 public Files Upload(HttpPostedFileBase files)
 {
    if (files.ContentLength > 0) {

    .....
 }

在HttpPostedFileBase类CONTENTLENGTH属性包含发布文件的字节数

这将使它所以你有一个文件上传框。

This will make it so you have a file uploading box available.

在ASP.NET WebForms的解决方案:

<asp:FileUpload ID="fuPictures" runat="server" />

请按钮用的OnClick或按需事件确实是这样的:

Make a button with a OnClick or OnCommand event that does something like this:

if (fuPictures.HasFile == true)
{
    int fileSize = fuPictures.FileBytes;
}

这会给你的文件大小。希望这有助于。

That will give you the file size. Hope this helps.

这篇关于验证之前上传文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆