[英]How to capture file upload data from web page external to an ASP.NET site using .NET
我很清楚如何使用標准ASP.NET技術將文件從客戶端發送到服務器,但是,我需要能夠從用基本html編寫的第三方網頁中檢索數據並處理文件數據在asp.net Web應用程序中。
因此,如果基本的html看起來像這樣...
<form id="form1" action="WebForm.aspx" method="post">
<input name="fileUpload1" type="file" enctype="multipart/form-data" />
<input type="submit" value="click" />
</form>
如何在表單的action屬性中引用的頁面中檢索文件數據。 到目前為止,我已經嘗試了下面的代碼,該代碼使我可以訪問文件名-但不能訪問文件的字節流。
protected void Page_Load( object sender, EventArgs e )
{
string fileName = Request.Form["fileUpload1"];
// No files appear in the request.files collection in code below.
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 );
}
}
任何建議,不勝感激。
您的表格不正確。 enctype
參數應位於form
標記上:
<form id="form1" action="WebForm.aspx" method="post" enctype="multipart/form-data">
<input name="fileUpload1" type="file" />
<input type="submit" value="click" />
</form>
如果您在Page_Load代碼期間嘗試從遠程(第三方)服務器檢索文件或資源,則無需使用文件上傳表單。
嘗試以下方法:
protected void Page_Load(object sender, EventArgs e) {
using(WebClient client = new WebClient()) {
var html = client.DownloadString("http://www.google.com/");
File.WriteAllText("filename", html);
}
}
由於這不是ASP.NET表單,並且您無法對其進行控制,因此需要使用第三方組件,例如Softartisans FileUp 。 我確信還有其他類似的控件。 “ 了解有關上傳文件的更多信息”中提到了其他一些信息! 頁。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.