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


 }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章