隨筆-7  評(píng)論-24  文章-102  trackbacks-0

          使用指定的方法將指定的本地文件上載到指定的資源

          在WinForm中通過(guò)HTTP協(xié)議向服務(wù)器端上傳文件


          1.服務(wù)器端處理程序
          <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Upload.aspx.cs" Inherits="Upload" %>
          前臺(tái)代碼  Upload.aspx


          using System;
          using System.Web;

          public partial class Upload : System.Web.UI.Page
          {
              
          //服務(wù)器默認(rèn)保存路徑
              private readonly string serverPath = @"C:\upload\";

              
          private void Page_Load(object sender, System.EventArgs e)
              
          {   // 獲取 http提交上傳的文件, 并改名保存
                  foreach (string key in Request.Files.AllKeys)
                  
          {
                      HttpPostedFile file 
          = Request.Files[key];
                      
          string newFilename = DateTime.Now.ToString("yyMMddhhmmssffff")
                          
          + file.FileName.Substring(file.FileName.LastIndexOf('.'));

                      
          try
                      
          {   //文件保存并返回相對(duì)路徑地址
                          file.SaveAs(this.serverPath + newFilename);
                          Response.Write(
          "upload/" + newFilename);
                      }

                      
          catch (Exception)
                      
          {                
                      }

                  }

              }

          }
          后臺(tái)代碼 Upload.aspx.cs
           



          2.客戶端程序
                 /// <summary>
                  
          /// 單個(gè)文件上傳至服務(wù)器
                  
          /// </summary>
                  
          /// <param name="uriAddress">接收文件資源的URI, 例如: http://xxxx/Upload.aspx</param>
                  
          /// <param name="filePath">要發(fā)送的資源文件, 例如: @"D:\workspace\WebService 相關(guān).doc</param>
                  
          /// <returns>返回文件保存的相對(duì)路徑, 例如: "upload/xxxxx.jpg" 或者出錯(cuò)返回 ""</returns>

                  private string UploadFile(string uriAddress, string filePath)
                  
          {
                      
          //利用 WebClient
                      System.Net.WebClient webClient = new System.Net.WebClient();
                      webClient.Credentials 
          = System.Net.CredentialCache.DefaultCredentials;
                      
          try
                      
          {
                          
          byte[] responseArray = webClient.UploadFile(uriAddress, "POST", filePath);
                          
          string savePath = System.Text.Encoding.ASCII.GetString(responseArray);
                          
          return savePath;
                      }

                      
          catch (Exception)
                      
          {
                          
          return "";
                      }

                  }



          ps.判斷遠(yuǎn)程文件是否存在
                  public bool UriExists(string url)
                  
          {
                      
          try
                      
          {
                          
          new System.Net.WebClient().OpenRead(url);
                          
          return true;
                      }

                      
          catch (System.Net.WebException)
                      
          {
                          
          return false;
                      }

                  }
           

          posted on 2009-05-26 08:57 黃小二 閱讀(5173) 評(píng)論(1)  編輯  收藏 所屬分類: C#

          評(píng)論:
          # re: 在 WinForm中使用 WebClient上傳文件 2014-11-26 17:28 | 熱熱
          44444444444444444444444  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 泰安市| 莱阳市| 乌拉特中旗| 中超| 察雅县| 宜都市| 静安区| 开远市| 博客| 和平区| 简阳市| 泸西县| 横山县| 松桃| 东海县| 阜南县| 太仆寺旗| 高安市| 黎城县| 财经| 揭阳市| 同仁县| 黎平县| 邵阳市| 黑山县| 洱源县| 南平市| 永修县| 青龙| 平阴县| 门源| 大冶市| 青海省| 上高县| 纳雍县| 宝鸡市| 金秀| 新平| 利辛县| 梅河口市| 永修县|