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.

3
  • stackoverflow.com/questions/3491174/… Commented Jan 11, 2012 at 9:24
  • It's not programmatic file save on client, user will initiate the process as mentioned in the post, If i understand that correctly. Commented Jan 11, 2012 at 9:28
  • Is this file hosted on IIS server inside some virtual directory? I hope this file doesn't contain important information. Commented Jan 11, 2012 at 9:29

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

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?
If this doesn't solve the problem, post the previous segment of code, where you use the file.
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.