I am creating an application in c# with mvc 2010 Express and at some point, the user can upload a file and open/download it to/from the database.
Here is the table in which I store the files:
CREATE TABLE [dbo].[tClientsFiles](
[IdClient] [int] NOT NULL,
[IdFile] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](256) NULL,
[Type] [varchar](256) NULL,
[Lenght] [money] NULL,
[Content] [varbinary](max) NULL,
[DateAdd] [datetime] NULL,
[UserAdd] [varchar](50) NULL)
In tClientsFiles.Name, I store the path and name of the file with: uploadFile.FileName;, where uploadFile is an HttpPostedFileBase. So for insance, if the file is Test.txt and it is in the User’s Desktop, it stores C:\Users\user\Desktop\test.txt
Then, when viewing/downloading the file, I have a link to an ActionResult in my Controller:
public ActionResult GetFile(int id)
{
var file = dre.tClientsFiles.First(m => m.IdFile == id);
MemoryStream ms = new MemoryStream(file.Content, 0, 0, true, true);
Response.ContentType = file.Type;
Response.AddHeader("content-disposition", "attachment;filename=" + file.Name);
Response.Buffer = true;
Response.Clear();
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();
return new FileStreamResult(Response.OutputStream, file.Type);
}
When opening the file, it names it with its “Name” file.Name, like this: c_Users_user_Desktop_Test.
My question is: is there a way (maybe modifying my Response.AddHeader) to name it with just its “real name”? (I mean, in our example: Test)
Or maybe a way to store the Name in a different way so that it just stores its “real name” instead of the name and its whole path?
Thanks a lot for your time!
Try