FileUpload 您需要亲自将 enctype="multipart/form-data" 添加到页面的 <form> 元素中 清单. FileUpload 控件生成的源代码
<
HTML
xmlns
="http://www.w3.org/1999/xHTML"
>
< head >< title > Upload Files </ title ></ head > < body > < form name ="form1" method ="post" action ="MyFileUpload.ASPx" id ="form1" enctype ="multipart/form-data" > < div > < input type ="hidden" name ="__VIEWSTATE" id ="__VIEWSTATE" value ="/wEPDwUJNDcxNTg5NDg3D2QWAgIEDxYCHgdlbmN0eXBlBRNtdWx0aXBhcnQvZm9yb S1kYXRhZGQUQEUFMY1+/fp1mnrkbqmVNQIzFA==" /> </ div > < div > < input type ="file" name ="FileUpload1" id ="FileUpload1" />< br /> < br /> < input type ="submit" name ="Button1" value ="Upload File" id ="Button1" /> < br /> < br /> < span id ="Label1" ></ span > </ div > < div > < input type ="hidden" name ="__EVENTVALIDATION" id ="__EVENTVALIDATION" value ="/wEWAgLB+7jIAwKM54rGBv2Iz6LxVY7jWec0gZMxnuaK2ufq" /> </ div ></ form > </ body > </ HTML >
确定可以上传:定位到保存上传的文件夹(用IE)——属性——安全——是否有ASPNET帐户 没则加 去掉上传大小限制:在 web.config.comments 文件(可以在 C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/CONFIG 的 ASP.NET 2.0 配置文件夹中找到)或应用程序的 web.config 文件中进行一些改动 使用验证控件限制上传类型
<
ASP:FileUpload ID
=
"
FileUpload1
"
runat
=
"
server
"
/><
br
/>
< br /> < ASP:Button ID = " Button1 " runat = " server " OnClick = " Button1_Click " Text = " Upload File " /> < br /> < br /> < ASP:Label ID = " Label1 " runat = " server " ></ ASP:Label > < ASP:RegularExpressionValidator id = " RegularExpressionValidator1 " runat = " server " ErrorMessage = " Only mp3, m3u or mpeg files are allowed! " ValidationExpression = " ^(([a-zA-Z]:)|(/{2}w+)$?)(/(w[w].*)) + (.mp3|.MP3|.mpeg|.MPEG|.m3u|.M3U)$ " ControlToValidate = " FileUpload1 " ></ ASP:RegularExpressionValidator > < br /> < ASP:RequiredFieldValidator id = " RequiredFieldValidator1 " runat = " server " ErrorMessage = " This is a required field! " ControlToValidate = " FileUpload1 " ></ ASP:RequiredFieldValidator > Protected Sub Button1_Click() Sub Button1_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) If FileUpload1.HasFile Then Dim fileExt As String fileExt = System.IO.Path.GetExtension(FileUpload1.FileName) If (fileExt = ".mp3") Then Try FileUpload1.SaveAs("C:Uploads" & _ FileUpload1.FileName) Label1.Text = "File name: " & _ FileUpload1.PostedFile.FileName & "" & _ "File Size: " & _ FileUpload1.PostedFile.ContentLength & " kb" & _ "Content type: " & _ FileUpload1.PostedFile.ContentType Catch ex As Exception Label1.Text = "ERROR: " & ex.Message.ToString() End Try Else Label1.Text = "Only .mp3 files allowed!" End If Else Label1.Text = "You have not specified a file." End If End Sub
检查服务器上的文件类型(C#)
|
ASP.NET使用FileUpload上传文件
最新推荐文章于 2020-04-25 08:42:04 发布
ASP.NET使用FileUpload上传文件