![](/img/trans.png)
[英]ASP.NET MVC Error while downloading large file at Response.Flush()
[英]Error in downloading a file in ASP.NET MVC
我正在ASP.NET MVC中工作。 我已經將文件存儲在數據庫中,現在我要下載並顯示其內容。 我正在分層工作。
這是我的代碼。
用於上傳文件的控制器動作
[HttpPost]
public ActionResult Edit(int id, UpdateAdvertisement model, HttpPostedFileBase file)
{
try
{
AdvertisementDTO add = new AdvertisementDTO();
add.DocImage = new byte[file.ContentLength];
add.ContentType = file.ContentType;
add.DocName = Convert.ToString(DateTime.Now.Ticks);
new AdvertisementHandler().Update(id, add);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
下載文件的控制器操作
public FileContentResult DownloadFile(int id)
{
string DocumentContentType = new AdvertisementHandler().DownloadContent(id);
string DocumentName = new AdvertisementHandler().DownloadDocumentName(id);
byte[] DocumentImage = new AdvertisementHandler().DownloadImage(id);
//return File(filename, contentType, "Report.pdf");
return File(DocumentImage, DocumentContentType, DocumentName);
//return File.ReadAllBytes(DocumentName);
}
業務邏輯層
這些是用於訪問數據庫的查詢。
public byte[] DownloadImage(int id)
{
byte[] file = (from f in db.TBL_ADVERTISEMENT
where f.ID == id
select new AdvertisementDTO
{
DocImage = f.DOCUMENT_IMG
}
).ToArray();
return file;
}
public string DownloadContent(int id )
{
string file = (from f in db.TBL_ADVERTISEMENT
where f.ID == id
select new AdvertisementDTO
{
ContentType = f.CONTENTTYPE
}
).ToString();
return file;
}
public string DownloadDocumentName(int id)
{
string file = (from f in db.TBL_ADVERTISEMENT
where f.ID == id
select new AdvertisementDTO
{
DocName = f.DOC_NAME
}
).ToString();
return file;
}
當我編譯此代碼時會出現此錯誤
錯誤1
無法將類型'ORS.DTO.AdvertisementDTO []'隱式轉換為'byte []'
F:\\ Projects \\在線招聘系統\\ ORS.BLL \\ AdvertisementHandler.cs 59 28 ORS.BLL
這是我的AdvertisementDTO ...
namespace ORS.DTO
{
public class AdvertisementDTO
{
public int ID { get; set; }
public string AddNumber { get; set; }
public string Description { get; set; }
public byte[] DocImage { get; set; }
public string ContentType { get; set; }
public string DocName { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public int StatusID { get; set; }
public virtual RecordStatusDTO RecordStatus { get; set; }
}
}
在對象上調用.ToArray()
不會將其轉換為字節數組。 您省略了AdvertisementDTO
的定義,因此我只能猜測它已經是一個字節數組。 如果不是這種情況,請發布AdvertisementDTO
的代碼,我將更新此帖子。
byte[] file = (from f in db.TBL_ADVERTISEMENT
where f.ID == id
select f.DOCUMENT_IMG).SingleOrDefault();
return file;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.