Silverlight文件上传下载实现方法(下载保存)

2019-12-30 11:12:34于丽
  • using System;  using System.Collections.Generic; 
  • using System.Linq;  using System.Web; 
  •   using System.IO; //因为要用到Stream 
  •   namespace HCLoad.Web 
  • {  public class WebClientUpLoadStreamHandler : IHttpHandler 
  • {   
  • public void ProcessRequest(HttpContext context)  { 
  • //获取上传的数据流  string fileNameStr = context.Request.QueryString["fileName"]; 
  • Stream sr = context.Request.InputStream;  try 
  • {  string filename = ""; 
  •   filename = fileNameStr; 
  •   byte[] buffer = new byte[4096]; 
  • int bytesRead = 0;  //将当前数据流写入服务器端文件夹ClientBin下 
  • string targetPath = context.Server.MapPath("Pics/" + filename + ".jpg");  using (FileStream fs = File.Create(targetPath, 4096)) 
  • {  while ((bytesRead = sr.Read(buffer, 0, buffer.Length)) > 0) 
  • {  //向文件中写信息 
  • fs.Write(buffer, 0, bytesRead);  } 
  • }   
  • context.Response.ContentType = "text/plain";  context.Response.Write("上传成功"); 
  • }  catch (Exception e) 
  • {  context.Response.ContentType = "text/plain"; 
  • context.Response.Write("上传失败, 错误信息:" + e.Message);  } 
  • finally  { sr.Dispose(); } 
  •   } 
  •   public bool IsReusable 
  • {  get 
  • {  return false; 
  • }  } 
  • }