本文介绍了ASP.NET C#-将FileStream保存在服务器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了初学者的问题.

I got beginners question.

如何将文件流保存到服务器上的文件?!

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

我知道了:

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

我的问题是,当我在ASP.NET Development Server中运行代码时,它将"asdasdasd.ico"保存到:C:\ Program Files \ Microsoft Visual Studio 9.0 \ Common7 \ IDE \ asdasdasd.ico

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.

我想将其保存到:www.my-web-site.com/output/asdasdasd.ico

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

非常感谢您.

推荐答案

使用 Server.MapPath() (也可以使用using块来关闭文件):

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

这篇关于ASP.NET C#-将FileStream保存在服务器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 12:53