Nour
Nour

Reputation: 5459

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 :

thanks

Upvotes: 1

Views: 646

Answers (1)

BonyT
BonyT

Reputation: 10940

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

 }

Upvotes: 4

Related Questions