HTMLフォームを使用してASPXでファイルをアップロードしようとしていて、コードビハインドでファイルをキャッチしようとしています。asp.netでファイルをアップロードできません
マイHTMLform今
<form id="form1" enctype="multipart/form-data">
<input type="file" id="myFile" name="myFile" />
<asp:Button runat="server" ID="btnUpload" OnClick="btnUploadClick" Text="Upload" />
</form>
そして、分離コードの私の方法です:
protected void btnUploadClick(object sender, EventArgs e)
{
HttpPostedFile file = Request.Files["myFile"];
//check file was submitted
if (file != null && file.ContentLength > 0)
{
string title = "title";
string message = "this worked";
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), title, "alert('" + message + "');", true);
string fname = Path.GetFileName(file.FileName);
file.SaveAs(Server.MapPath(Path.Combine("~/App_Data/", fname)));
}
else
{
string title = "title";
string message = "nothing happened";
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), title, "alert('" + message + "');", true);
}
}
私は私のHTMLフォームを送信するたびだから、私はいつも何も起こらなかった警告を取得します。助言がありますか?
おかげ
を取り除く:あなたはそれを追加すると予想されるように、それが動作するはず? – David