首页 新闻 会员 周边 捐助

附件上传不了,代码哪里出问题了?

0
悬赏园豆:10 [已解决问题] 解决于 2018-05-10 10:48

 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
JenasYang的主页 JenasYang | 初学一级 | 园豆:146
提问于:2018-02-05 14:07
< >
分享
最佳答案
0

已经自己解决:将

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

后,已可正常上传附件。

JenasYang | 初学一级 |园豆:146 | 2018-02-05 17:16
其他回答(2)
0

改为postedFile.SaveAs(Server.MapPath("upload/pubfile/" + dreate + "/") + FileNames + fileExtension)试试

收获园豆:10
dudu | 园豆:29594 (高人七级) | 2018-02-05 14:37

无效。

支持(0) 反对(0) JenasYang | 园豆:146 (初学一级) | 2018-02-05 14:40

@JenasYang: 具体出现什么错误?

支持(0) 反对(0) dudu | 园豆:29594 (高人七级) | 2018-02-05 14:42

@dudu: 没报错,就是点击【上传附件】按钮后,下面列表中没有显示上传的附件,附件没有传上来

支持(0) 反对(0) JenasYang | 园豆:146 (初学一级) | 2018-02-05 14:44

@JenasYang: 建议打点看一下files.Count的值

支持(0) 反对(0) dudu | 园豆:29594 (高人七级) | 2018-02-05 14:53
0

也许是差了个这个 multipart/form-data http://www.w3school.com.cn/tags/att_form_enctype.asp

nil | 园豆:886 (小虾三级) | 2018-02-05 17:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册