简体   繁体   中英

Downloading files in asp.net using C#

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM