开发者

where should a asp.net website saves uploaded files and how to make a download link for them?

I have a asp.net web application using MVC framework, the website allows the registrars to upload some identification documents.

I want the best option to upload these documents :

  • option 1- make a directory in the website folder and save its name in the website web.config file, and save the document file name in my database, so when I want to compose download link I have to combine the root url and the folder name and the document file name.

    for example http://www.website.com + "/DoucmentsFolder" + "/Documentfilename "

  • option 2- save the entire file in the database, and each time I need the file to be downloaded, I have to save it to temporary folder and compose a link to the file, and make开发者_StackOverflow中文版 sure to delete the file after the user has gotten the file.
  • may be there are other suggestion from you .

thanks


The issues with using a FileStore are multiple:

1) Clean-up - you need to leave your file there long enough to be requested and collected, but how do you clean up? If you don't you run the risk of running out of space. The best way is to have a scheduled task run every so often and delete files older than say an hour - but this is still messy.

2) Doesn't scale well - unless you write to a SAN - in a web-garden/farm scenario the users request for the file may come to a different server than that which stored it - unless you constrain your system to force all requests from a user to use the same server.

3) Not performant - compared with database operation.

So I would say database for sure - and it's far easier to do this than you illustrate.

Instead of creating a file and a link to it, return the file as a FileContentResult:

 public FileContentResult GetFile(int fileId)
{
        myFile MyFile = fileRepository.Get(fileId);
        return MyFile(file.Data, file.MimeType);
    }
}

where MyFile looks like:

 public class MyFile
 {
    public MyFile(byte[] fileData, string mimeType)
    {
       this.FileData = fileData;
       this.MimeType = mimeType;
    }

    public virtual int Id {get; private set;}
    public virtual byte[] FileData { get; set; }
    public virtual string MimeType { get; set; }
}

To Store the File

 [HttpPost]
 public ActionResult SaveFile(HttpPostedFileBase userFile)
 {
    MyFile myFile = new MyFile(new byte[userFile.ContentLength], userFile.ContentType)
    userFile.InputStream.Read(MyFile.FileData, 0, userFile.ContentLength);

    fileRepository.Save(myFile);

 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜