0

I have a formed file in some folder on server. I need to create some solution to allow user save this file local on drive on computer.

Can anyone advice me how it can be done, what control I should use.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Darien Fawkes
  • 3,023
  • 7
  • 24
  • 35
  • **http://stackoverflow.com/questions/3491174/how-to-save-a-file-generated-in-server-machine-in-clients-machinemydocument** – huMpty duMpty Jan 11 '12 at 09:24
  • It's not programmatic file save on client, user will initiate the process as mentioned in the post, If i understand that correctly. – MUS Jan 11 '12 at 09:28
  • Is this file hosted on IIS server inside some virtual directory? I hope this file doesn't contain important information. – Amar Palsapure Jan 11 '12 at 09:29

2 Answers2

2

This will open the Browser SaveAs dialog:

 protected void Page_Load(object sender, EventArgs e)
{  

    FileStream fs = File.OpenRead(Server.MapPath("~/imgName.jpg"));
    byte[] buffer = new byte[(int)fs.Length];
    fs.Read(buffer, 0, (int)fs.Length);
    fs.Close();
    SetResponse("imgName");
    HttpContext.Current.Response.BinaryWrite(buffer);
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.Close();
}

private static void SetResponse(string fileName)
{
    string attachment = "attachment; filename=" + fileName + ".jpg";
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ClearHeaders();
    HttpContext.Current.Response.ClearContent();
    HttpContext.Current.Response.AddHeader("content-disposition", attachment);
    HttpContext.Current.Response.ContentType = "image/jpeg";
    HttpContext.Current.Response.AddHeader("Pragma", "public");
}

try opening the FileStream with this permissions:

  FileStream fs = new FileStream(Server.MapPath("~/imgName.jpg"), FileMode.Open, FileAccess.Read, FileShare.Read);
zdrsh
  • 1,667
  • 1
  • 14
  • 26
  • Exactly what I needed. But now on FileStream fs = File.OpenRead(Server.MapPath("~/imgName.jpg")); it returns exception: the file already used! Can I to go round that? – Darien Fawkes Jan 11 '12 at 10:20
  • If this doesn't solve the problem, post the previous segment of code, where you use the file. – zdrsh Jan 11 '12 at 10:53
  • the file NameDB.mdf file, the data base. It used all over the project. I try to allow some users to copy data base file. – Darien Fawkes Jan 11 '12 at 13:18
  • http://stackoverflow.com/questions/321077/cant-access-file-because-it-is-being-used-by-another-process – zdrsh Jan 11 '12 at 13:31
1

You will just need to put a link of your file in aspx page.

<a href="path to your file on server">some text here</a>

When user click on this link they will got the download dialog box using which they can save the file to there local system.

MUS
  • 1,450
  • 4
  • 17
  • 30