1 Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click 2 Dim fujian As String = "" 3 Dim dreate As String = System.DateTime.Today 4 If Not Directory.Exists(Server.MapPath("upload/pubfile/") + dreate) Then 5 Directory.CreateDirectory(Server.MapPath("upload/pubfile/") + dreate) 6 End If 7 8 Dim files As HttpFileCollection = HttpContext.Current.Request.Files 9 Dim strmsg As System.Text.StringBuilder = New System.Text.StringBuilder("上传的错误信息分别为:<hr color=red>") 10 11 Dim filecount As Int16 = files.Count 12 Try 13 For filecount = 0 To filecount 14 Dim postedFile As HttpPostedFile = files(filecount) 15 Dim fileName, fileExtension As String 16 fileName = System.IO.Path.GetFileName(postedFile.FileName) 17 If fileName <> String.Empty Then 18 fileExtension = System.IO.Path.GetExtension(fileName) 19 If System.IO.Path.GetExtension(fileExtension).ToLower() = ".jpg" Or System.IO.Path.GetExtension(fileExtension).ToLower() = ".gif" Or System.IO.Path.GetExtension(fileExtension).ToLower() = ".pdf" Or System.IO.Path.GetExtension(fileExtension).ToLower() = ".doc" Or System.IO.Path.GetExtension(fileExtension).ToLower() = ".xls" Or System.IO.Path.GetExtension(fileExtension).ToLower() = ".ppt" Or System.IO.Path.GetExtension(fileExtension).ToLower() = ".tif" Or System.IO.Path.GetExtension(fileExtension).ToLower() = ".tiff" Or System.IO.Path.GetExtension(fileExtension).ToLower() = ".rar" Then 20 Dim FileNames As String = Replace(Date.Today, "-", "") & Replace(TimeOfDay, ":", "") & filecount 21 If File.Exists(Server.MapPath("upload/pubfile/") + dreate + "/" + FileNames + fileExtension) Then 22 FileNames = FileNames & Replace(TimeOfDay, ":", "") 23 End If 24 postedFile.SaveAs(Server.MapPath("upload/pubfile/") + dreate + "/" + FileNames + fileExtension) 25 fujian = dreate + "/" + FileNames + fileExtension 26 End If 27 End If 28 Next 29 Catch ex As Exception 30 ex.Message.Trim() 31 End Try 32 ListBox1.Items.Add(fujian) 33 End Sub
已经自己解决:将
Dim FileNames As String = Replace(Date.Today, "-", "") & Replace(TimeOfDay, ":", "") & filecount
修改为
Dim FileNames As String = Date.Today.Year & Date.Today.Month & Date.Today.Day & Replace(TimeOfDay, ":", "") & filecount
后,已可正常上传附件。
改为postedFile.SaveAs(Server.MapPath("upload/pubfile/" + dreate + "/") + FileNames + fileExtension)
试试
无效。
@JenasYang: 具体出现什么错误?
@dudu: 没报错,就是点击【上传附件】按钮后,下面列表中没有显示上传的附件,附件没有传上来
@JenasYang: 建议打点看一下files.Count
的值
也许是差了个这个 multipart/form-data http://www.w3school.com.cn/tags/att_form_enctype.asp