Durante il tentativo di implementare un file MVC, caricare example sul blog di Scott Hanselman. Mi sono imbattuto in problemi con questo codice di esempio:Come faccio a trasmettere da System.Web.HttpPostedFileBase a System.Web.HttpPostedFile?
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);
}
ho convertito a VB.NET:
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
Ma io sono sempre un'eccezione getto non valida dal compilatore:
Value of type 'System.Web.HttpPostedFileBase' cannot be converted to 'System.Web.HttpPostedFile'.
Hanselman ha pubblicato il suo esempio nel 2008-06-27 e presumo abbia funzionato all'epoca. MSDN non ha esempi simili, quindi cosa dà?
Questo ha funzionato, grazie. –
INFORMAZIONI AGGIUNTIVE: se sei come me e stai creando questa funzione in un progetto separato, devi includere il file System.Web.Abstractions.dll per fare riferimento a HttpPostedFileBase per: http://efreedom.com/Question/1-1911151/CSharp-Reference-HttpPostedFileBase –
@tvanfosson sto cercando queste informazioni, perché HttpPostedFileBase invece di HttpPostedFile? Hai un link che puoi condividere? – codingbiz