求在ASP.NET中上传图片的VB.NET源码
急用,谢谢!
[解决办法]
Imports System.IO
添加控件FileUpload ,名字为:FileUpload1
在“上传”button下面的code
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim filePath As String = FileUpload1.PostedFile.FileName
Dim h As String = Path.GetExtension(Path.GetFileName(filePath))
Dim fileSavePath As String = Server.MapPath( " ") & "\Photo\ " & "文件保存的名字.jpg "
FileUpload1.PostedFile.SaveAs(fileSavePath)
End Sub
[解决办法]
'全部原码,自己复制后调试下吧.
<Script Language= "VB " Runat= "Server ">
Sub Upload(sender As Object, e As Eventargs)
Dim I, fileCount As Byte, tempName, tempSize, tempType As String
For I = 0 To Request.Files.Count - 1
Dim myFile As HttpPostedFile = Request.Files(I)
If myFile.ContentLength > 0 Then
fileCount += 1
Dim Route() As String = Split(myFile.FileName, "\ ")
Dim FileName As String = Route(Route.Length - 1)
myFile.SaveAs(Server.MapPath( ". ") & "\Files\ " & FileName)
tempName = tempName & "〖 " & FileName & "〗 "
tempsize = tempsize & "〖 " & Math.Round(myFile.ContentLength / 1024, 2) & "KB〗 "
tempType = tempType & "〖 " & myFile.ContentType & "〗 "
End If
If fileCount = 0 Then
Result.Text = " <Font Color= 'Red '> 上传失败或指定的文件不存在。 </Font> <Hr Size= '1 ' Color= 'Green '> "
FileInfo.Visible = False
Else
FileInfo.Visible = True
Result.Text = " <Font Color= 'Green '> 上传成功! </Font> <Hr Size= '1 ' Color= 'Green '> "
FNumber.Text = fileCount
FName.Text = tempName
FSize.Text = tempSize
FType.Text = tempType
End If
Next
End Sub
</Script>
<Html>
<Head>
<Title> 千山月工作室 </Title>
</Head>
<Body BgColor= "#ffffcc ">
<Center> <H3> <Font Color= "Green "> 文件上传 </Font> </H3> </Center>
<Form Runat= "Server " Enctype= "multipart/form-data ">
<Center>
<Input Type= "File " Runat= "Server " Size= "50 "> <Br>
<Input Type= "File " Runat= "Server " Size= "50 "> <Br>
<Input Type= "File " Runat= "Server " Size= "50 "> <Br>
<Input Type= "File " Runat= "Server " Size= "50 "> <Br>
<Input Type= "File " Runat= "Server " Size= "50 "> <Br> <Br>
<Asp:Button Runat= "Server " Text= "上传文件 " OnClick= "Upload " />
<Input Type= "Reset " Value= "重新设置 ">
<Hr Size= "1 " Color= "Green ">
<Asp:Label Runat= "Server " Id= "Result " />
</Center>
<Asp:Panel Runat= "Server " Id= "FileInfo " Visible= "False ">
文件数目: <Asp:Label Runat= "Server " Id= "FNumber " /> <Br>
文件名称列表: <Asp:Label Runat= "Server " Id= "FName " /> <Br>
文件大小列表: <Asp:Label Runat= "Server " Id= "FSize " /> <Br>
文件类型列表: <Asp:Label Runat= "Server " Id= "FType " />
</Asp:Panel>
</Form>
</Body>
</Html>
[解决办法]
最好不要把图片转化成数据库,因为当图片一多,数据库就变胖了,速度、性能就下降了!用图片的路径来保存图片比较好!Varchar类型