在ASP.NET MVC中下载文件时出错 [英] Error in downloading a file in ASP.NET MVC

查看:65
本文介绍了在ASP.NET MVC中下载文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET MVC工作。我现在已经在数据库中存储了一个文件,我想下载并显示其内容。我正在分层工作。



这是我的代码。



用于上传文件的控制器动作< br $>


 [HttpPost] 
public ActionResult Edit(int id,UpdateAdvertisement model,HttpPostedFileBase file)
{
尝试
{
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);
返回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);
// 返回文件(filename,contentType,Report.pdf);
return 文件(DocumentImage,DocumentContentType,DocumentName);
// 返回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 文件;
}

public string DownloadContent( int id)
{
string file =( from f db.TBL_ADVERTISEMENT
其中 f.ID == id
选择 new AdvertisementDTO
{
ContentType = f.CONTENTTYPE
}
).ToString();
return 文件;
}

public string DownloadDocumentName( int id)
{
string file =( from f db.TBL_ADVERTISEMENT
其中 f.ID == id
选择 new AdvertisementDTO
{
DocName = f.DOC_NAME
}
).ToString();
return 文件;
}



编译此代码时出现此错误



错误1无法隐式转换类型'ORS.DTO.AdvertisementDTO []'到'byte []'



F:\Projects \Online Recruitment System \ORS.BLL \AdvertisementHandler。 cs 59 28 ORS.BLL

这是我的广告DTO ...



<前lang =cs> namespace ORS.DTO
{
public class AdvertisementDTO
{
public int ID {获得; set ; }
public string AddNumber { get ; set ; }
public string 描述{ 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 {获得; set ; }
}
}

解决方案

好吧,你遇到了很多问题。首先,您的代码永远不会将图像保存到数据库中。它会保存您创建的所有图像元数据,但图像数据本身永远不会保存到数据库中。您创建一个字节数组文件内容的长度,但永远不会将该内容放在字节数组中。



< pre lang = cs> byte [] file =(来自db.TBL_ADVERTISEMENT中的f 
其中f.ID == id
选择新的广告DTO
{
DocImage = f.DOCUMENT_IMG
}
)。ToArray();



错误信息非常清楚问题所在。您正在将数据结构转换为字节数组,而不是AdvertisementDTO对象的DocImage字段中的文件内容。


您需要这样的内容



  public   byte  [] DownloadImage(< span class =code-keyword> int  id)
{
var dto =(来自 f db.TBL_ADVERTISEMENT
其中​​ f.ID = = id
select new AdvertisementDTO
{
DocImage = f。 DOCUMENT_IMG
}
).FirstOrDefault();

if (dto == null
{
return null ;
}
else
{
return dto。 DocImage;
}
}





如前所述,你实际上并没有真正保存图像,这样可以解决您的编译问题,但不能解决您的整体问题。


I am working in ASP.NET MVC. I have stored a file in the database now I want to download and display its contents. I am working in layers.

Here is my code.

Controller Action used for uploading file

[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();
    }
}



Controller Action for downloading file

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);
    }



Business Logic Layer

These are the queries that are used to access database.

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;
}


This error arises when i compile this code

Error 1 Cannot implicitly convert type 'ORS.DTO.AdvertisementDTO[]' to 'byte[]'

F:\Projects\Online Recruitment System\ORS.BLL\AdvertisementHandler.cs 59 28 ORS.BLL
Here is my 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; }
    }
}

解决方案

Well, you've got a bunch of problems. First, your code never saves the image to the database. It saves all the image metadata that you created, but the image data itself is never saved to the database. You create a byte array the length of the file content, but never put that content in the byte array.

<pre lang="cs">byte[] file = (from f in db.TBL_ADVERTISEMENT
       where f.ID == id
       select new AdvertisementDTO
       {
           DocImage = f.DOCUMENT_IMG
       }
       ).ToArray();


The error message is very clear about the the problem is. You're converting your data structure to a byte array, not the file content in the DocImage field of the AdvertisementDTO object.


You need something like this

public byte[] DownloadImage(int id)
{
    var dto = (from f in db.TBL_ADVERTISEMENT
       where f.ID == id
       select new AdvertisementDTO
       {
           DocImage = f.DOCUMENT_IMG
       }
       ).FirstOrDefault();

     if (dto == null)
     {
         return null;
     }
     else
     {
         return dto.DocImage;
     }
}



as mentioned already though, you're not actually saving the image in the first place, so that might solve your compilation issue but not your overall problem.


这篇关于在ASP.NET MVC中下载文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆