ASP.NET C# - 在服务器上保存 FileStream

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

问题描述

我有初学者的问题.

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

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 开发服务器中运行代码时,它会将asdasdasd.ico"保存到:C:Program FilesMicrosoft Visual Studio 9.0Common7IDEasdasdasd.ico

My problem is than when I run code in ASP.NET Development Server it saves "asdasdasd.ico" into: C:Program FilesMicrosoft Visual Studio 9.0Common7IDEasdasdasd.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)