HttpPostedFile 和 HttpPostedFileBase 你真的瞭解嘛?

來源:互聯網
上載者:User

     當你看到的時候,你是不是已經愛上了它,如果你真的只看外表,那你就錯了,不要太相信自己的眼睛,往往真像並不是你所看到的那麼簡單!請跟我一起來看看吧!

     這次在項目中,就遇到了這個問題,剛開始我還天真的以為他們真的有關係,沒有到都是假象的。

     遇到的問題:“把圖片上傳到資原始伺服器”的一個上傳問題,剛開始做的時候沒考慮到,代碼如下:

    

public bool UploadFTP(HttpPostedFileBase file, string strFileType, int iFileLength, int Width, int Height, string Path, ref string strInfo){   ............. //     }

     本以為這樣就已經是通用的了,當我直接傳HttpPostedFile 對象的時候報錯了。答案是 HttpPostedFileHttpPostedFileBase 不存在關係。

     所以我只好,到晚上去找,去查,終於找到了一個好的解決方案,其實它們還是可以通過一個橋樑 HttpPostedFileWrapper 類來轉化,HttpPostedFileWrapper : HttpPostedFileBaseHttpPostedFileWrapper 的代碼如下:

public class HttpPostedFileWrapper : HttpPostedFileBase{        // 摘要:        //     初始化 System.Web.HttpPostedFileWrapper 類的新執行個體。        //        // 參數:        //   httpPostedFile:        //     通過此封裝類可訪問的對象。        //        // 異常:        //   System.ArgumentNullException:        //     httpApplicationState 為 null。        public HttpPostedFileWrapper(HttpPostedFile httpPostedFile);}

 

最後解決方案如下:

 public bool UploadFTP(HttpPostedFile file, string strFileType, int iFileLength, int Width, int Height, string Path, ref string strInfo)
{ HttpPostedFileBase hpfb = new HttpPostedFileWrapper(file) as HttpPostedFileBase; return UploadFTP(hpfb, strFileType, iFileLength, Width, Height, Path, ref strInfo);}

 每天進步一點點,一年累積的進步,你去好好想想吧!

 您的支援是我最大的動力,如果你覺得還可以,請您給個”推薦“!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.