如何在MVC ASP.NET中增加视频上传的最大限制 [英] How do I increase maximum limit of video upload in MVC ASP.NET

查看:80
本文介绍了如何在MVC ASP.NET中增加视频上传的最大限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



我一直在尝试上传在我的MVC项目中有大空间的Mp4视频而且它给了我一个错误,它只允许视频小于3MB。即使我更改下面的if语句,例如

Hello

I've been trying to upload Mp4 videos which have large space in my MVC project and it gives me an error, it only allows videos that are smaller then 3MB. Even if i change the if statement below e.g

if(videofile.ContentLength< 104857600)

仍然无法解决我的问题。



请帮助



我尝试过:



still it doesn't solve my problem.

Please help

What I have tried:

videofilesDB db =new videofilesDB();
        GET: MVCupload
        public ActionResult Index()
        {
           return View();
        }
        public ActionResult Index(HttpPostedFileBase videofile)
        {
            if(videofile!=null)
            {
                string filename = Path.GetFileName(videofile.FileName);
                if(videofile.ContentLength< 104857600)
                {
                    videofile.SaveAs(Server.MapPath("/Videofiles/" + filename));

                    string mainconn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                    SqlConnection sqlconn = new SqlConnection(mainconn);
                    string sqlquery = "insert into [dbo].[videofiles] values (@Vname,@Vpath)";
                    SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
                    sqlconn.Open();
                    sqlcomm.Parameters.AddWithValue("@Vname", filename);
                    sqlcomm.Parameters.AddWithValue("@Vpath","/Videofiles/" + filename);
                    sqlcomm.ExecuteNonQuery();
                    sqlconn.Close();
                    ViewData["Message"] = "Record Saved Successfully!";

                }
            }
            return View("Index");

推荐答案

很多像IIS这样的Web服务器都有你需要调整的最大请求长度属性。此设置可防止更大的请求在IIS管道中弹回,甚至在它到达您的代码之前。您可以在IIS中的网站属性中以及 web.config 中调整此设置。



不久前,我写了一篇关于使用实体框架在SQL Server数据库中存储图像的文章,使用EF和ASP.NET在SQL Server中存储图像 [ ^ ]



有一个阅读,它为你节省了很多代码,如上所述
A lot of webservers like IIS have this maximum request length property you need to adjust. This setting prevents requests larger to be bounced in the IIS pipeline even before it hits your code. You can adjust this setting in your website properties in IIS, and in your web.config.

By the way, a while ago I wrote this article about storing images in a SQL Server database using entity framework, Storing Images in SQL Server using EF and ASP.NET[^]

Have a read, it saves you a lot code like above


试试这个;



在你的web.config文件中<&的System.Web GT;部分添加

Try this;

In your web.config file in the <system.web> section add
<!--The default size is 4096 kilobytes (4 MB). MaxValue is 2147483647 KB (2 TB)-->
<!-- 100 MB in kilobytes -->
<httpRuntime maxRequestLength="102400" />





和< system.webserver>部分添加





and in the <system.webserver> section add

<security>
   <requestFiltering>
       <!--The default size is 30000000 bytes (28.6 MB). MaxValue is 4294967295 bytes (4 GB)-->
       <!-- 100 MB in bytes -->
        <requestLimits maxAllowedContentLength="104857600" />
    </requestFiltering>
</security>


这篇关于如何在MVC ASP.NET中增加视频上传的最大限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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