文件上传控件——WebbUpload
在.aspx中,添加一个<input type="file">控件,代码如下:
这段代码中要注意,encType="multipart/form-data"这个属性一定要加入。另外,input中不必加入runat="server"。完成了这步之后,你可以在用于触发上传文件的button中绑定两个事件处理函数。
不要忘了在.cs文件中声明这个命名空间:using Webb.WAVE.Controls.Upload;
有时候可能出现UploadFile ifUp1 = webbUpload.GetUploadFile("ifUp1");这句话中的ifUp1返回一个空引用。这是由于你没有在web.config中加入下面这句:
以及在form中加入那句encType="multipart/form-data"。
1<form id="Form1" method="post" encType="multipart/form-data" runat="server">
2 <input id="ifUp1" contentEditable="false" type="file" name="ifUp1">
3</form>
2 <input id="ifUp1" contentEditable="false" type="file" name="ifUp1">
3</form>
这段代码中要注意,encType="multipart/form-data"这个属性一定要加入。另外,input中不必加入runat="server"。完成了这步之后,你可以在用于触发上传文件的button中绑定两个事件处理函数。
1private void ibtnSend_Load(object sender, System.EventArgs e)
2 {
3 ImageButton ibtnSender = (ImageButton)sender;
4 WebbUpload webbUpload = new WebbUpload();
5 webbUpload.RegisterProgressBar(ibtnSender);
6 }
7
8private void ibtnSend_Click(object sender, System.Web.UI.ImageClickEventArgs e)
9 {
10string uploadFolder = MapPath(@"Upload\");
11 WebbUpload webbUpload = new WebbUpload();
12 UploadFile ifUp1 = webbUpload.GetUploadFile("ifUp1");
13
14if (ifUp1 != null)
15 {
16 string fileName1 = Path.GetFileName(ifUp1.ClientFullPathName);
17 ifUp1.SaveAs(uploadFolder + newFileName);
18 }
19 }
20
2 {
3 ImageButton ibtnSender = (ImageButton)sender;
4 WebbUpload webbUpload = new WebbUpload();
5 webbUpload.RegisterProgressBar(ibtnSender);
6 }
7
8private void ibtnSend_Click(object sender, System.Web.UI.ImageClickEventArgs e)
9 {
10string uploadFolder = MapPath(@"Upload\");
11 WebbUpload webbUpload = new WebbUpload();
12 UploadFile ifUp1 = webbUpload.GetUploadFile("ifUp1");
13
14if (ifUp1 != null)
15 {
16 string fileName1 = Path.GetFileName(ifUp1.ClientFullPathName);
17 ifUp1.SaveAs(uploadFolder + newFileName);
18 }
19 }
20
不要忘了在.cs文件中声明这个命名空间:using Webb.WAVE.Controls.Upload;
有时候可能出现UploadFile ifUp1 = webbUpload.GetUploadFile("ifUp1");这句话中的ifUp1返回一个空引用。这是由于你没有在web.config中加入下面这句:
1<!-- Settings of WebbUpload -->
2 <httpRuntime useFullyQualifiedRedirectUrl="true" maxRequestLength="1024000" executionTimeout="900" />
3 <httpModules>
4 <add name="WebbHttpModule" type="Webb.WAVE.Controls.Upload.WebbHttpModule, Webb.WAVE.Controls.Upload"/>
5 </httpModules>
6 <httpHandlers>
7 <add verb="*" path="progress.ashx" type="Webb.WAVE.Controls.Upload.WebbUploadStatusHandler, Webb.WAVE.Controls.Upload" />
8 </httpHandlers>
9 <!-- ========================= -->
2 <httpRuntime useFullyQualifiedRedirectUrl="true" maxRequestLength="1024000" executionTimeout="900" />
3 <httpModules>
4 <add name="WebbHttpModule" type="Webb.WAVE.Controls.Upload.WebbHttpModule, Webb.WAVE.Controls.Upload"/>
5 </httpModules>
6 <httpHandlers>
7 <add verb="*" path="progress.ashx" type="Webb.WAVE.Controls.Upload.WebbUploadStatusHandler, Webb.WAVE.Controls.Upload" />
8 </httpHandlers>
9 <!-- ========================= -->
以及在form中加入那句encType="multipart/form-data"。