关于stream的问题。web services 传递附件。请高手指点!

harron 2003-07-21 04:20:28
wse中有这样的例子,
[WebMethod]
public void CreateDimedImage()
{
SoapContext respContext = HttpSoapContext.ResponseContext;
DimeAttachment dimeAttach = new DimeAttachment(
"image/gif", TypeFormatEnum.MediaType,
@"C:\images\test.gif");
respContext.Attachments.Add(dimeAttach);
}
-----------------------
调用
Private void btnGetImage_Click(object sender, System.EventArgs e)
{
MyDimeService svc = new MyDimeService();
svc.CreateDimedImage();
if (svc.ResponseSoapContext.Attachments.Count == 1)
{
MessageBox.Show("Got it!\n");
pbDime.Image = new Bitmap(
svc.ResponseSoapContext.Attachments[0].Stream);
}
}
--------------------------------
我想问的是,如果是其他类型的文件,比如文本文件,word文件等,如何写一个通用的函数呢?
调用函数直接将 数据 保存为 文件。
--------------------------------
或者,svc.ResponseSoapContext.Attachments[0].Stream 类型就是stream,
我想请问,直接将stream类型的数据写成一个文件,如何操作???
我这样写了下。有问题,我对流弄不明白。唉
StreamWriter objWriter=new StreamWriter(@"d:\good.txt",true,Encoding.GetEncoding("gb2312"));
const int size = 4096;
byte[] bytes = new byte[4096];
int numBytes;
Stream str =svc.ResponseSoapContext.Attachments[0].Stream;
while((numBytes = str.Read(bytes, 0, size)) > 0) <---需要byte类型

objWriter.Write(bytes,0,numBytes); <---需要char类型
objWriter.Close();

...全文
32 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
cyp503 2003-07-21
  • 打赏
  • 举报
回复
private void CreateFile(string path,System.IO.Stream stream)
{
if(path==null || path=="")
throw new Exception("文件路径不能为空");
if(stream==null)
throw new Exception("附件为空!");
try
{
System.IO.BinaryReader reader = new System.IO.BinaryReader(stream,System.Text.Encoding.Default);
System.IO.BinaryWriter writer = new System.IO.BinaryWriter(System.IO.File.Open(path.Trim() ,System.IO.FileMode.Create ));
if(writer!=null)
{

#region 保存文件
Byte[] bytes = new Byte[4096];
int pos=0;
while(true)
{
int cc=reader.Read(bytes,0,4096);
if(cc>0)
{
writer.Write(bytes,0,cc);
pos+=cc;
}
else
break;
}
reader.Close();
writer.Close();

#endregion
}
}
catch(Exception ex)
{
throw(ex);
}

}

62,133

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧