vb.net附件上传函数,请大家指正
Private Function SaveImages() As System.Boolean
'遍历File表单元素
Dim files As System.Web.HttpFileCollection = System.Web.HttpContext.Current.Request.Files
Dim FilePath, newpath As String
Dim ls_sql As String
Dim fileName, fileExtension As System.String
'状态信息
Dim strMsg As New System.Text.StringBuilder("上传的文件分别是:<hr color=red>")
Dim iFile As System.Int32
Try
For iFile = 0 To files.Count - 1
'检查文件扩展名字
Dim postedFile As System.Web.HttpPostedFile = files(iFile)
fileName = System.IO.Path.GetFileName(postedFile.FileName)
If Not (fileName = String.Empty) Then
fileExtension = System.IO.Path.GetExtension(fileName)
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>")
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>")
strMsg.Append("上传文件的文件名:" + fileName + "<br>")
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>")
'可根据扩展名字的不同保存到不同的文件夹
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("attach/") + Me.tsubjectid.Text + fileName)
FilePath = Server.MapPath("attach/img/") + fileName
newpath = "http://" + Context.Request.Url.Host + context.Request.ApplicationPath + "/knowledge/attach" + "/" + Me.tsubjectid.Text + fileName + ""
newpath = newpath.Replace("/", "/")
'new_path.Text = newpath
End If
If (fileName = String.Empty) Then
msgbox(Me, "请添加附件!")
Exit Function
End If
Dim put_file As System.Int32
ls_sql = "insert into knowledge_main_attach(accname,accpath,tsubjectid) "
ls_sql += "values ('" & fileName & "','" & newpath & "','" + Me.tsubjectid.Text + "')"
With New data_deal
If (.ExcuteSql(ls_sql) < 0) Then
msgbox(Me, " 附件没有成功!")
End If
End With
Next
strStatus.Text = strMsg.ToString()
Return True
Catch Ex As System.Exception
strStatus.Text = Ex.Message
Return False
End Try
End Function