0

I got beginners question.

How to save a file stream to a file on server?!

I got this:

var source = Request.QueryString["src"];
WebClient webclient = new WebClient();
using (Stream stream = webclient.OpenRead(source))
{
   Bitmap iconbitmap = new Bitmap(System.Drawing.Image.FromStream(stream));
   FileStream os = new FileStream("./asdasdasd.ico", FileMode.Create);

   var ic = MakeIcon(iconbitmap, 16, false); /// function returns an icon

   ic.Save(os);
}

My problem is than when I run code in ASP.NET Development Server it saves "asdasdasd.ico" into: C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\asdasdasd.ico

And, when I run it on remote server got error.

I want to save it into: www.my-web-site.com/output/asdasdasd.ico

Thank you very much.

2
  • Are you trying to upload a file from the client to a page on your server? If so, this is not the right way to do it. Commented Sep 7, 2011 at 21:57
  • No, I do not need to upload. I need to get source image from other server/site via QueryString, convert it to an icon and save it to server. BrokenGlass solution did the job :) Commented Sep 7, 2011 at 22:10

3 Answers 3

3

Use Server.MapPath() (also a using block for closing the file would help):

using(FileStream os = new FileStream(Server.MapPath("/output/asdasdasd.ico"), FileMode.Create))
{
   var ic = MakeIcon(iconbitmap, 16, false); /// function returns an icon
   ic.Save(os);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need to map your server's path :

If memory serves me right, you can use something like :

HttpContext.Current.Server.MapPath("~/MyPathToSomething");

Note that the path string must start with the tilde character.

Regards

Comments

1

You can use code to get the website's directory and form your own file path.

See this question for how to get the website's directory.

How to get website's physical path on local IIS server? (from a desktop app)

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.