C#中文件大小服务器端的限制 [英] Limit on a file size server side in C#

查看:238
本文介绍了C#中文件大小服务器端的限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在MVC4中上传文件,但是在我的控制器中,我试图将文件大小限制为最大4MB,但收到以下警告

I am currently uploading files in MVC4 but in my controller I tried to limit the file size to 4MB max but got the following warning

comparison to integral constant is useless

使用 Haacks 示例

[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
if (file.ContentLength > 0)
{
if (file.ContentLength < 4000000000 )
{
   var fileName = System.IO.Path.GetFileName(file.FileName);
   var path = System.IO.Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
   file.SaveAs(path);
 }

运行代码时出现错误

Maximum request length exceeded.

由于file.ContentLengthint,我无法控制它,所以不知道如何解决.谁能帮助我限制服务器端的文件大小.

Since file.ContentLength is an int over which I have no control I do not know what is the way around this. Can anyone help me on how to limit the file size server side.

编辑对于可能要指出40000000004gb的人来说,您是正确的.但是,即使我将其替换为4000000,我的操作也会成功完成,但不会保存文件,因为我的If()不满意,即file.ContentLength < 4000000返回false.

Edit To those that might want to point out that 4000000000 is 4gb you are correct. But even if I replace it with 4000000 my action completes sucessfully but does not save the file as my If() is not satisfied i.e file.ContentLength < 4000000 returns false.

编辑这不是一个重复的问题,我想将文件大小限制为一定的限制,而不忽略IIS中的文件大小限制.

Edit This is not a duplicate question I want to limit my file size to a certain limit not ignore the file size limit in IIS.

推荐答案

在您的web.config中设置此值

Set this value in your web.config

<httpRuntime maxRequestLength="4096"/>

这篇关于C#中文件大小服务器端的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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