Asp.Net MVC上传大于2MB的视频文件



我有一个API解决方案(目标框架4.0)和一个MVC解决方案(目标框架4.5.1)的作业门户。在概要中,我需要上传视频,但我面临的问题是,我无法上传大小大于2MB的视频文件。所以,我在互联网上搜索,找到一个解决方案是改变web.config关于Api解决方案,

Web.config
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime executionTimeout="108000" maxRequestLength="2147483647"/>
</system.web>
<system.webServer>
<!-- maxAllowedContentLength = 2GB (the value is in Bytes) -->
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483647" />
</requestFiltering>
</security>
</system.webServer>

应用后出现同样的问题"Failed!远程服务器返回一个错误:(500)内部服务器错误。">

这里我的控制器代码MVC解决方案CandidateController

#region UPDATE CANDIDATE PROFILE SUMMERY
[HttpPost]
public string UpdateProfileSummery(string info)
{
string Res = "";
string Token = "";string FileName="";
ResultInfo<string> ResultInfo = new Models.ResultInfo<string>();
CandidateProfileSummery ProfInfo = new CandidateProfileSummery();
try
{
if (info != "" && info != null)
{                    
ProfInfo = JsonConvert.DeserializeObject<CandidateProfileSummery>(info);

//ProfInfo.Gender = ProfInfo.NamePrefix == "Mr." ? "male" : "female";
if (Request.Files.Count > 0)
{
//  Get all files from Request object  
HttpFileCollectionBase files = Request.Files;
if (User.Identity.Name != "" && User.Identity.Name != null)
{
string[] arr = User.Identity.Name.Split('|');
Token = arr[0];
ProfInfo.ID = Convert.ToInt64(arr[3]);
}
for (int i = 0; i < files.Count; i++)
{
HttpPostedFileBase file = files[i];
int fileSizeInBytes = file.ContentLength;
FileName = file.FileName;
if (file.FileName.ToLower().Contains(".mp4")|| file.FileName.ToLower().Contains(".mov") || file.FileName.ToLower().Contains(".wmv") || file.FileName.ToLower().Contains(".avi") || file.FileName.ToLower().Contains(".flv") || file.FileName.ToLower().Contains(".mkv") || file.FileName.ToLower().Contains(".3gp"))
{
ProfInfo.VideoType = Path.GetExtension(file.FileName);
if (ProfInfo.VideoType != ".mp4" && ProfInfo.VideoType != ".MP4" && ProfInfo.VideoType != ".MOV" && ProfInfo.VideoType != ".mov" && ProfInfo.VideoType != ".WAV" && ProfInfo.VideoType != ".wmv" && ProfInfo.VideoType != ".AVI" && ProfInfo.VideoType != ".avi" && ProfInfo.VideoType != ".FLV" && ProfInfo.VideoType != ".flv" && ProfInfo.VideoType != ".MKV" && ProfInfo.VideoType != ".mkv" && ProfInfo.VideoType != ".3GP" && ProfInfo.VideoType != ".3gp")
{
Res = "Failed! File type should be video file.";
}
else
{                                    
MemoryStream target = new MemoryStream();
file.InputStream.CopyTo(target);
byte[] fileData = target.ToArray();
ProfInfo.VD = fileData;
}
}
else
{
ProfInfo.FileType = Path.GetExtension(file.FileName);
if (ProfInfo.FileType != ".png" && ProfInfo.FileType != ".jpeg" && ProfInfo.FileType != ".jpg" && ProfInfo.FileType != ".gif")
{
Res = "Failed! File type should be jpeg/jpg or png.";
}

else
{

MemoryStream target = new MemoryStream();
file.InputStream.CopyTo(target);
byte[] fileData = target.ToArray();
ProfInfo.DP = fileData;
}
}


}
string fname;
if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
{
string[] testfiles = FileName.Split(new char[] { '\' });
fname = testfiles[testfiles.Length - 1];
}
else
{
//In this section I got error when file size > 2MB
string JsonRes = WebService.Postfile(ApiUrl + "Jobseeker/updateJbsProfileSummery?Token=" + Token, JsonConvert.SerializeObject(ProfInfo));
ResultInfo = JsonConvert.DeserializeObject<ResultInfo<string>>(JsonRes);
if (ResultInfo.ErrorCode == 200)
{
Res = "Success! " + ResultInfo.Info;
}
else
{
Res = ResultInfo.Info;
}
}
}
else
{
if (User.Identity.Name != "" && User.Identity.Name != null)
{
string[] arr = User.Identity.Name.Split('|');
Token = arr[0];
ProfInfo.ID = Convert.ToInt64(arr[3]);
}
string JsonRes = WebService.Postfile(ApiUrl + "Jobseeker/updateJbsProfileSummery?Token=" + Token, JsonConvert.SerializeObject(ProfInfo));
ResultInfo = JsonConvert.DeserializeObject<ResultInfo<string>>(JsonRes);
if (ResultInfo.ErrorCode == 200)
{
Res = "Success! " + ResultInfo.Info;
}
}
}
else
{
Res = "Failed! Information is not provided.";
}

}
catch (Exception ex)
{
Res = "Failed!" + ex.Message;
}
return Res;
}
#endregion

Api解决方案web配置文件"maxRequestLength"目标框架4.0不支持如果有人解决了我的问题,谢谢你在先进

添加此配置

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = 52428800; //50MB
});

更多信息请访问此链接,点击这里

相关内容

  • 没有找到相关文章

最新更新