1

How do i download a file in asp.net? here is what i did to upload it: I upload the file into the website and saved the url to it in a database like this:

string CVPath = null;
  if (uploadfiles.HasFile)
  {
    string file = uploadfiles.FileName;
    uploadfiles.PostedFile.SaveAs(Server.MapPath(".") + "//CV//" + file);
    CVPath = "~//ProfileImages//" + file;
    FileName.InnerText = file;
  }
  else
    CVPath = "";

and then I save the "CVPath" in a database

Kamal Saeed
  • 105
  • 2
  • 11
  • It's not clear what you're asking - you're mixing up downloading and uploading, and you have posted code that you don't say whether it works or not. – mason Apr 04 '15 at 13:41
  • there is more than one way to upload a file so i wrote how i did it to help you to know what is the best way to do a download for that file, and yes it work!! however I just want to know how to download a file! – Kamal Saeed Apr 05 '15 at 10:25

1 Answers1

0

To download a file, first you need to read all the contents to a string.

    MemoryStream ms = new MemoryStream();
    TextWriter tw = new StreamWriter(ms);
    tw.WriteLine("YourString");
    tw.Flush();
    byte[] bytes = ms.ToArray();
    ms.Close();
    Response.Clear();
    Response.ContentType = "application/force-download";
    Response.AddHeader("content-disposition", "attachment; filename=file.txt");
    Response.BinaryWrite(bytes);
    Response.End();