我正在使用Visual Studio 15.我的应用程序需要上传大小为6MB的文件。我正在使用.Net 4.5.1与实体框架6.Visual Studio没有响应上传6MB大小的文件
下面的脚本和html代码被写在视图级剃须刀来选择和检查文件的上传。
$('#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>
下面的代码被写入控制器到达要上传的文件并显示相应的消息。
[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);
}
}
以下函数用于在sql数据库表中插入一个名为Responses的行。
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;
}
}
一个响应表称为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的文件,它不显示任何错误消息甚至不是内存不足。但行和文件已保存。为什么它没有显示任何消息,虽然它正在上传文件?
Visual Studio 15:您的意思是VS2015(v14.x)还是VS2017(v15.x)? –
我看到你有客户端,服务器端和SQL Server逻辑。您是否能够调试以检查三个组件中的前两个或后两个之间的问题? –
问题是服务器没有发送消息给客户端 –