-1

I'm using WebMatrix. I uploaded a abc.doc file to a folder. A hyperlink is created to download the file. Example: <a href="~/uploads/StudyMaterial/abc.doc">.

When I click on the link, the file will be downloaded with name abc.doc. How can I rename it to Hello.doc when downloading the file?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • You need a file server/handler to send the file to the client and give it a new name. If you link directly to abc.doc then IIS just sends the file directly, so you don't have an option to rename it. Can you please give more detail about what you've tried and what you're looking to accomplish? – Eli Gassert Feb 25 '13 at 15:24

1 Answers1

0

In order to rename the file you could set the filename attribute when adding the Content-Disposition header to the response:

Response.AppendHeader("Content-Disposition", "attachment; filename=\"Hello.doc\"");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • You will need a Razor page to serve your files. Then you will reference this page: `@k.Title`. Inside this page you will stream the file to the response and add the Content-Disposition header which will allow you to change its name. – Darin Dimitrov Feb 25 '13 at 15:45
  • I tried with Response.WriteFile("~/uploads/StudyMaterial/25.docx"); Response.AppendHeader("Content-Disposition","attachment; filename=hheelo.docx"); When i tried to open the downloaded file, it said the file is corrupted. Is it i use the wrong method? – user2107797 Feb 26 '13 at 03:10
  • This is the correct approach. Try clearing the Response buffer (`Response.Clear()`) before writing the file. – Darin Dimitrov Feb 26 '13 at 06:48
  • Only docx file cannot be opened directly. Txt, doc, ppt, pdf are no problem. – user2107797 Feb 26 '13 at 10:25
  • Are you sure that the file is not already corrupted on the server? Are you able to open it directly from the folder? – Darin Dimitrov Feb 26 '13 at 10:27
  • I can open it directly from the folder. – user2107797 Feb 26 '13 at 10:49
  • I added Response.End(); Response.Flush(); Now the problem solved. Thanks. – user2107797 Feb 26 '13 at 11:14