webservice上传下载2

 

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.IO;

namespace WebService1
{
 /// <summary>
 /// Service1 的摘要说明。
 /// </summary>
 public class Service1 : System.Web.Services.WebService
 {
  public Service1()
  {
   //CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
   InitializeComponent();
  }

  #region 组件设计器生成的代码
  
  //Web 服务设计器所必需的
  private IContainer components = null;
    
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if(disposing && components != null)
   {
    components.Dispose();
   }
   base.Dispose(disposing);  
  }
  
  #endregion

  // WEB 服务示例
  // HelloWorld() 示例服务返回字符串 Hello World
  // 若要生成,请取消注释下列行,然后保存并生成项目
  // 若要测试此 Web 服务,请按 F5 键

//  [WebMethod]
//  public string HelloWorld()
//  {
//   return "Hello World";
//  }
  [WebMethod(Description="上传并保存图片文件")]
  public bool SaveFile(byte[] binData,string fileName)
  {
   bool success=false;  

//   string savePath=System.Configuration.ConfigurationSettings.AppSettings["UploadDirectory"];
//   if(savePath==null) savePath="Photo";
//   if(savePath.IndexOf(":\\")<0) savePath=Server.MapPath(savePath);//不是绝对路径
//   if(!savePath.EndsWith("\\")) savePath += "\\";
//
//   if(!Directory.Exists(savePath))
//   {
//    throw new Exception("服务器端没有找到有效的保存路径!");
//   }
  
   FileStream fileStream=null;
   try
   {
    string savePath="c:\\";
    fileStream=new FileStream(savePath + fileName,FileMode.Create,FileAccess.Write);
    //write the file
    fileStream.Write(binData,0,binData.Length);
    fileStream.Flush();//clear the buffer,write the data to the hard disk
    success=true;
   }
   catch(Exception ex)
   {
    throw new Exception(ex.Message);
   }
   finally
   {
    fileStream.Close();
   }
   return success;
  }
 
  [WebMethod(Description = "为了支持多点分块异步上传文件,此方法必须由客户端预先调用,以便在服务器端生成指定 FileName 和 Length 大小的空白文件预定空间! 建议客户端同步调用")]
  public string CreateBlankFile(string FileName,int Length) //建议由客户端同步调用
  {
   FileStream fs = new FileStream(Server.MapPath(".") + "\\" + FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
   fs.Write(new byte[Length], 0, Length);
   fs.Close();
   fs = null;
   return FileName + " (" + Length + ") 空白文件已经创建!";
  }

  [WebMethod(Description = "提供一个用于一次完整上传整个文件的方法! 建议客户端同步调用")]
  public string UploadFileBytes(byte[] Bytes,string FileName)
  {
   return UploadFileChunkBytes(Bytes, 0, FileName);
  }

  [WebMethod(Description = "提供一个用于一次只上传由 Position 位置起始的, Bytes 字节的 FileName 文件块存入服务器端相应文件的相应字节位置! 建议客户端异步调用")]
   // 这里只要多提供一个 Position 参数,余下的再由客户端调用异步的该方法,就轻松达到目的了!
  public string UploadFileChunkBytes(byte[] Bytes,int Position,string FileName)
  {
   try
   {
    FileStream fs = new FileStream(Server.MapPath(".") + "\\" + FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
    //该 Bytes 的字节要写到 服务器端 相应文件的从 Position 开始的字节
    fs.Position = Position;
    fs.Write(Bytes, 0, Bytes.Length);
    fs.Close();
    fs = null;
    return FileName + " 文件块: 位置[" + Position + "," + (Position + Bytes.Length) + "] 大小(" + Bytes.Length + ") 上传成功!";
   }
   catch (Exception e)
   {
    return e.Message;
   }
  }

  [WebMethod]
  public byte[] DownloadFileBytes(string FileName)
  {

   if (File.Exists(Server.MapPath(".") + "\\" + FileName))
   {
    try
    {
     FileStream fs = File.OpenRead(Server.MapPath(".") + "\\" + FileName);
     int i = (int) fs.Length;
     byte[] ba = new byte[i];
     fs.Read(ba,0,i);
     fs.Close();
     return ba;
    }
    catch
    {
     return new byte[0];
    }
   }
   else
   {
    return new byte[0];
   }
  }


 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值