Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 上传前验证文件大小_C#_Asp.net Mvc 3_File_File Upload - Fatal编程技术网

C# 上传前验证文件大小

C# 上传前验证文件大小,c#,asp.net-mvc-3,file,file-upload,C#,Asp.net Mvc 3,File,File Upload,我需要验证要上传到服务器的文件。必须在上载之前完成验证。i、 e.在客户端完成验证。此任务应在ASP.NET MVC3网页中完成。它还应该适用于所有浏览器。IE9,8,7/FF/铬。我开始知道IE没有FileReader API 我的问题是,如何在上传到MVC3网页之前验证文件大小。对于支持HTML 5的浏览器,可以使用简单的javascript轻松实现: Html语法 <input type="file" id="myFile" /> 但是,对于较旧的浏览器(我们都在期待您的In

我需要验证要上传到服务器的文件。必须在上载之前完成验证。i、 e.在客户端完成验证。此任务应在ASP.NET MVC3网页中完成。它还应该适用于所有浏览器。IE9,8,7/FF/铬。我开始知道IE没有FileReader API


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

对于支持HTML 5的浏览器,可以使用简单的javascript轻松实现:

Html语法

<input type="file" id="myFile" />
但是,对于较旧的浏览器(我们都在期待您的Internet Explorer),在客户端执行此操作的唯一方法是使用ActiveX:

var myFile = document.getElementById('myFile');

var myFSO = new ActiveXObject("Scripting.FileSystemObject");
var filepath = myfile.file.value;
var thefile = myFSO.getFile(filepath);
var size = thefile.size;
    alert(size + " bytes");

.Net MVC解决方案:

<span>
<b>Attachment</b> (8 MB only)<label id="attached" style="color:red; margin-left:5px"></label>
</span>
<input type="file" id="Attachment" name="Attachment" class="admin_textfeildmedium" value="" style="width:551px" accept="image/*">
我使用的数据类型是
HttpPostedFileBase

在您的
Views>Shared
文件夹中,创建一个名为“EditorTemplates”的新文件夹,并使用此文件夹:

@model HttpPostedFileBase

@Html.TextBox("", null, new { type = "file" })
然后,我将此
HttpPostedFileBase
对象从控制器传递给执行以下操作的方法:

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

    .....
 }
HttpPostedFileBase类上的ContentLength属性包含发布文件中的字节数

这将使你有一个文件上传框可用

在ASP.NET WebForms解决方案上:

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

这将为您提供文件大小。希望这有帮助。

您可以通过使用jquery实现:

<span>
<b>Attachment</b> (8 MB only)<label id="attached" style="color:red; margin-left:5px"></label>
</span>
<input type="file" id="Attachment" name="Attachment" class="admin_textfeildmedium" value="" style="width:551px" accept="image/*">
#

附件(仅8MB)
#
jQuery(文档).ready(函数(){
jQuery(“#附件”).bind('change',function(){
//fileUpload=0;
var iSize=(this.files[0].size/1024);
如果(iSize/1024>1){
如果((iSize/1024)/1024)大于1){
fileUpload=0;
}否则{
iSize=(数学四舍五入((iSize/1024)*100)/100);

如果(iSize@Praveen),这对你有帮助吗?
<span>
<b>Attachment</b> (8 MB only)<label id="attached" style="color:red; margin-left:5px"></label>
</span>
<input type="file" id="Attachment" name="Attachment" class="admin_textfeildmedium" value="" style="width:551px" accept="image/*">
jQuery(document).ready(function () {


jQuery('#Attachment').bind('change', function () {
                            //fileUpload = 0;
                            var iSize = (this.files[0].size / 1024);
                            if (iSize / 1024 > 1) {
                                if (((iSize / 1024) / 1024) > 1) {
                                    fileUpload = 0;
                                } else {
                                    iSize = (Math.round((iSize / 1024) * 100) / 100);
                                    if (iSize <= 8) {
                                        fileUpload = 1;
                                    } else {
                                        fileUpload = 0;
                                    }
                                }
                            } else {
                                fileUpload = 1;
                            }
                            if (fileUpload == 0) {
                               // alert("Your attachment exceeded 8MB.");
                                jQuery('#attached').html('Your attachment exceeded 8MB.');
                                jQuery('#Attachment').val('');
                            }

                        });

                    });