2009-05-11 22 views
12

Scott Hanselman'ın blogunda bir MVC dosyası yüklemeye çalışırken example. Ben VB.NET onu dönüştürülürSystem.Web.HttpPostedFileBase'den System.Web.HttpPostedFile'a nasıl döküm uygularım?

foreach (string file in Request.Files) 
{ 
    HttpPostedFile hpf = Request.Files[file] as HttpPostedFile; 
    if (hpf.ContentLength == 0) 
     continue; 
    string savedFileName = Path.Combine(
     AppDomain.CurrentDomain.BaseDirectory, 
     Path.GetFileName(hpf.FileName)); 
    hpf.SaveAs(savedFileName); 
} 

:

For Each file As String In Request.Files 
    Dim hpf As HttpPostedFile = TryCast(Request.Files(file), HttpPostedFile) 
    If hpf.ContentLength = 0 Then 
     Continue For 
    End If 
    Dim savedFileName As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(hpf.FileName)) 
    hpf.SaveAs(savedFileName) 
Next 

Ama derleyici geçersiz döküm özel durum alıyorum:

Value of type 'System.Web.HttpPostedFileBase' cannot be converted to 'System.Web.HttpPostedFile'. 

Ben bu örnek kodu ile belaya girdi Hanselman, örneğini 2008-06-27'de yayınladı ve o zamanlar çalıştığını düşünüyorum. MSDN benzer örneklere sahip değil, bu yüzden ne veriyor?

cevap

27

Sadece bir HttpPostedFileBase gibi onunla çalışmak. Çerçeve, bir HttpPostedFile öğesini bir HttpPostedFileBase nesnesine dönüştürmek için HttpPostedFileWrapper'ı kullanır. HttpPostedFile, birim testi zor olan mühürlü sınıflardan biridir. Örnek yazıldıktan sonra, MVC çerçevesinde test etme yeteneğini (HttpPostedFileBase kullanarak) geliştirmek için sarmalayıcı kodunu uyguladıklarından şüpheleniyorum. Denetleyicideki HttpContext, HttpRequest ve HttpReponse özellikleriyle benzer şeyler yapılmıştır.

+0

Bu çalıştı, teşekkürler. –

+2

EK BİLGİ: Beni seviyorsanız ve bu işlevi ayrı bir projede oluşturuyorsanız, HttpPostedFileBase öğesine başvuruda bulunmak için System.Web.Abstractions.dll dosyasını eklemeniz gerekir: http://efreedom.com/Question/1-1911151/CSharp-Reference-HttpPostedFileBase –

+0

@tvanfosson bu bilgiyi arıyorum, neden HttpPostedFile yerine HttpPostedFileBase? Paylaşabileceğin bir bağlantın var mı? – codingbiz

4

Kullanılacak doğru tür HttpPostedFileBase'dir.

HttpPostedFileBase hpf = Request.Files[file]; 
+0

Sana inanıyorum ama referansın var mı? HttpPostedFileBase ve HttpPostedFile arasındaki fark hakkında biraz daha fazla okumak isterim. – Trevor