我正在将文件上传到共享文件系统。到目前为止一切顺利,在我上传文件的最后一步,就是将fileName添加到无序列表中。
我的问题是我注意到它正在接收每个文件并在循环中运行两次。由于安全问题,我无法附加文件(大多数上传网站在我们的系统上被阻止)但是我会将结果放在块引用中。
我还附上了我的代码。
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim uploads As HttpFileCollection
uploads = HttpContext.Current.Request.Files
For i As Integer = 0 To (uploads.Count - 1)
If (uploads(i).ContentLength > 0) Then
Dim c As String = System.IO.Path.GetFileName(uploads(i).FileName)
Dim fileSize As Integer = uploads(i).ContentLength
If fileSize > 1048576 Then
Span1.InnerHtml = "This file exceeds the allowed file size (1 MB). Please resize the file or select another file."
BulletedList.Items.Add("The File " & ControlChars.Quote & c & ControlChars.Quote & " - was larger than (1 mb). Please resize the file or select another file. ")
ElseIf fileSize < 5 Then
Span1.InnerHtml = "This file does not contain enough data. Please upload a bigger file."
BulletedList.Items.Add("The File " & ControlChars.Quote & c & ControlChars.Quote & " does not contain enough data. Please upload a bigger file. ")
Else
Try
uploads(i).SaveAs("C:\filePath\" + c)
BulletedList.Items.Add("The File " & ControlChars.Quote & c & ControlChars.Quote & " was Uploaded Sucessfully.")
Span1.InnerHtml = "File Uploaded Sucessfully."
Catch Exp As Exception
Span1.InnerHtml = "Some Error occured."
End Try
End If
End If
Next i
End Sub
End Class
我的成功文件输出:
文件已成功上传。
- 文件“正常 - 复制(2).txt”已成功上传。
- 文件“正常 - 复制(2).txt”已成功上传。
我再也看不出为什么它会在循环中运行两次。是否存在我没有看到的逻辑错误?
答案 0 :(得分:1)
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
在原始字符串中。那Handles btnSubmit.Click
导致事件发生两次。简单地改为:
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
特别感谢蒂姆