C# Visual studio没有响应上载大小为6MB的文件
我正在Visual studio 15上工作。我的应用程序需要上传一个6MB大小的文件。我将.NET4.5.1与EntityFramework6一起使用 下面的脚本和html代码是在视图级别编写的,用于选择和检查文件的上载C# Visual studio没有响应上载大小为6MB的文件,c#,entity-framework-6,httppostedfilebase,C#,Entity Framework 6,Httppostedfilebase,我正在Visual studio 15上工作。我的应用程序需要上传一个6MB大小的文件。我将.NET4.5.1与EntityFramework6一起使用 下面的脚本和html代码是在视图级别编写的,用于选择和检查文件的上载 $('#SaveResponse').click(function () { var data = new FormData(); var files = $("#imagefile").get(0).files; if (files.length > 0) {
$('#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>
responses表中名为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 confif文件的设置如下
<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的文件,即使内存不足,程序也不会显示任何错误消息。但行和文件已保存。为什么在上传文件时不显示任何消息?如果我们直接操作文件流,问题可以通过以下方式解决
public int insertStreamResponse(HttpPostedFile file)
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = "Data
Source=HOME\\MSQLEXPRESS2016;Initial Catalog=Evaluation;Integrated
Security=True";
sqlConnection.Open();
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandType = CommandType.Text;
sqlCommand.CommandText = "insert into responses values
(ResponseId, (0X))";
SqlParameter parameter;
parameter = new System.Data.SqlClient.SqlParameter("@ResponseId",
System.Data.SqlDbType.UniqueIdentifier);
parameter.Value = this.response.ResponseId;
sqlCommand.Parameters.Add(parameter);
SqlCommand helperCommand = new SqlCommand();
sqlCommand.Transaction = sqlConnection.BeginTransaction();
sqlCommand.ExecuteNonQuery();
helperCommand.Connection = sqlConnection;
helperCommand.Transaction = sqlCommand.Transaction;
helperCommand.CommandText = "SELECT
GET_FILESTREAM_TRANSACTION_CONTEXT()";
Object transactionContext = helperCommand.ExecuteScalar();
helperCommand.CommandText = "SELECT ResponseImage.PathName() FROM
Responses WHERE [ResponseId] = @Id";
parameter = new System.Data.SqlClient.SqlParameter("@Id",
System.Data.SqlDbType.UniqueIdentifier);
helperCommand.Parameters.Add(parameter);
helperCommand.Parameters["@Id"].Value = sqlCommand.Parameters
["@ResponseId"].Value;
string filePathInServer = (string)helperCommand.ExecuteScalar();
byte[] buffer = new byte[file.ContentLength];
//read from the input file
Stream fileStream = file.InputStream;
if (fileStream.CanRead)
fileStream.Read(buffer, 0, file.ContentLength);
else return 0;
//write into the file stream
SqlFileStream sqlFileStream = new SqlFileStream(filePathInServer,
(byte[])transactionContext, System.IO.FileAccess.Write);
if (sqlFileStream.CanWrite)
sqlFileStream.Write(buffer, 0, file.ContentLength);
else return 0;
sqlCommand.Transaction.Commit();
sqlConnection.Close();
return 1;
}
如果您使用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以千字节为单位测量,这就是此配置示例中的值不同的原因。(两者都相当于1GB。)Visual Studio 15:您是指VS2015(v14.x)还是VS2017(v15.x)?我知道您有客户端、服务器端和SQL server逻辑。您是否能够调试以检查三个组件中的前两个或后两个之间是否存在问题?问题是服务器没有向客户端发送消息这意味着回答或修改您的问题?
<configuration> <system.web> <httpRuntime maxRequestLength="1048576" /> </system.web> </configuration>
<system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="1073741824" /> </requestFiltering> </security> </system.webServer>