我正在使用 Visual Studio 15。我的应用程序需要上传一个 6MB 大小的文件。我正在使用 .Net 4.5.1 和 Entity Framework 6。
以下脚本和 html 代码是在 View 级 razor 中编写的,用于选择和检查文件的上传。
$('#SaveResponse').click(function () {
var data = new FormData();
var files = $("#imagefile").get(0).files;
if (files.length > 0) {
data.append("UploadedImage", files[0]);
}
else
{
alert('You have not selected any File');
return;
}
$.ajax({
async: false,
cache: false,
type: "POST",
dataType: "json",
contentType: false,
processData: false,
url: "@(Url.RouteUrl("UploadFile"))",
data: data,
success: function (JsonCP) {
if (JsonCP.msg != null) {
if (JsonCP.msg.Key) {
alert(JsonCP.msg.Value);
$("#fileUpload").val('');
} else
alert(JsonCP.msg.Value);
}
},
error: function (JsonCP) {
alert(JsonCP.msg.Value);
}
});
});
<table>
<tr>
<td align="right" width="200px">@Html.Label("Select the File:")</td>
<td align="left" width="200px"><input type="file" id="imagefile" />
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="button" value="Save this
Response" id="SaveResponse" name="SaveResponse" /></td>
</tr>
</table>
在 Controller 中编写了以下代码,以获取要上传的文件并显示相应的消息。
[System.Web.Mvc.AcceptVerbs(HttpVerbs.Post)]
public ActionResult UploadFile()
{
UploadResponseModel rm = new UploadResponseModel();
try
{
if (System.Web.HttpContext.Current.Request.Files.AllKeys.Any())
{
var httpPostedFile =
System.Web.HttpContext.Current.Request.Files["UploadedImage"];
if (httpPostedFile != null)
{
string fileSavePath =
Path.Combine(HttpContext.Server.MapPath("~/UploadedFiles"),
Path.GetFileName(httpPostedFile.FileName));
httpPostedFile.SaveAs(fileSavePath);
rm.responseModel.response.ResponseImage =
System.IO.File.ReadAllBytes(filesavePath)
if (rm.responseModel.insertResponse() != 1)
rm.msg = new KeyValuePair<bool, string>(false,
"row is not saved successfully.");
else
rm.msg=new KeyValuePair<bool,string>(true,"File
uploaded Successfully.");
}
else
rm.msg = new KeyValuePair<bool, string>(false, "Could not
get the file.");
}
else
rm.msg = new KeyValuePair<bool, string>(false, "No file found to
Upload.");
}
catch (Exception ex)
{
rm.msg = new KeyValuePair<bool, string>(false, "Can not Upload the
file: " + ex.Message);
}
return Json(rm, JsonRequestBehavior.AllowGet);
}
}
以下函数用于在名为 Responses 的 sql 数据库表中插入一行。
public int insertResponse()
{
using (Entities cntx = new Entities())
{
cntx.Responses.Add(this.response);cntx.Entry(this.response).
State = System.Data.Entity.EntityState.Added;
int ex=cntx.SaveChanges();
return ex;
}
}
response 表的一列 responseImage 是 fileStream 的数据类型。它的另一列是 uniqueIdentifier 类型。下面给出建表sql。
CREATE TABLE [dbo].[Responses] (
[ResponseId] UNIQUEIDENTIFIER ROWGUIDCOL NOT NULL,
[ResponseImage] VARBINARY (MAX) FILESTREAM NULL,
CONSTRAINT [PK_Responses] PRIMARY KEY CLUSTERED ([ResponseId] ASC)
FILESTREAM_ON [FileStreamGroup], UNIQUE NONCLUSTERED ([ResponseId] ASC)
);
Web conf文件是这样设置的
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5.1" maxRequestLength="3145728"
executionTimeout="9999999" useFullyQualifiedRedirectUrl="false"
minFreeThreads="8" minLocalRequestFreeThreads="4"
appRequestQueueLimit="1000"/>
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="3221225472" />
</requestFiltering>
</security>
该程序可以正常运行并为小文件显示正确的消息,但对于大小为 3 MB 的文件它不会显示任何错误消息甚至内存不足。但是行和文件被保存了。为什么上传文件没有显示任何信息?
最佳答案
如果您使用 IIS 托管您的应用程序,则默认上传文件大小为 4MB。要增加它,请在您的 web.config 中使用以下部分 -
<configuration> <system.web> <httpRuntime maxRequestLength="1048576" /> </system.web> </configuration>
对于IIS7及以上版本,还需要添加以下行:
<system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="1073741824" /> </requestFiltering> </security> </system.webServer>
注意:maxAllowedContentLength 以字节为单位,而 maxRequestLength 以千字节为单位,这就是此配置示例中值不同的原因。 (两者都相当于 1 GB。)
关于c# - Visual Studio 没有响应上传大小为 6MB 的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46490929/