做了一个从客户端读取文件并显示在Web页上的简单程序.代码如下:
    Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
        Dim strFilename As String()
        strFilename = filUpload.PostedFile.FileName.Split("\")
        filUpload.PostedFile.SaveAs(Request.MapPath(Request.ApplicationPath) & "\" & strFilename(UBound(strFilename)))
        lstServerFiles.Items.Add(strFilename(UBound(strFilename)))
        lstServerFiles.SelectedIndex = 0
    End Sub    Private Sub btnView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnView.Click
        Dim strFilename As String
        strFilename = lstServerFiles.SelectedItem.ToString
        Response.Redirect(Request.ApplicationPath & "/" & strFilename)
    End Sub运行时点击Upload按钮出现错误在:
filUpload.PostedFile.SaveAs(Request.MapPath(Request.ApplicationPath) & "\" & strFilename(UBound(strFilename)))点击View按钮出现错误在:
strFilename = lstServerFiles.SelectedItem.ToString之前在Html的<form>标签中加入了 encType="multipart/form-data" 属性.各位高手谁能帮我解决这个问题啊...

解决方案 »

  1.   

    给你一个
    页面
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm3.aspx.vb" Inherits="CreateControl.WebForm3"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>WebForm3</title>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    <style type="text/css">TD { FONT-SIZE: 9pt }
    </style>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" encType="multipart/form-data" runat="server">
    <center>
    <h4><strong>上传多图到数据库</strong></h4>
    </center>
    <table borderColor="#ffffff" cellSpacing="0" borderColorDark="#3366ff" cellPadding="0"
    width="70%" align="center" borderColorLight="#ccccff" border="1">
    <tr>
    <td align="center">图片一</td>
    <td><input id="File0" type="file" runat="server"></td>
    </tr>
    <tr>
    <td align="center">图片二</td>
    <td><input id="File1" type="file" name="File2" runat="server"></td>
    </tr>
    <tr>
    <td align="center">图片三</td>
    <td><input id="File2" type="file" name="File3" runat="server"></td>
    </tr>
    <tr>
    <td align="center">图片四</td>
    <td><input id="File3" type="file" name="File4" runat="server"></td>
    </tr>
    <tr>
    <td align="center" colSpan="2"><asp:button id="btnSave" Runat="server" Text="上 传"></asp:button></td>
    </tr>
    <tr>
    <td align="center" colspan="2">
    <table align="center" border="0" cellpadding="0" width="100%">
    <tr>
    <td align="center">
    <asp:Image ID="image0" Runat="server"></asp:Image>
    </td>
    </tr>
    <tr>
    <td align="center">
    <asp:Image ID="image1" Runat="server"></asp:Image>
    </td>
    </tr>
    <tr>
    <td align="center">
    <asp:Image ID="image2" Runat="server"></asp:Image>
    </td>
    </tr>
    <tr>
    <td align="center">
    <asp:Image ID="image3" Runat="server"></asp:Image>
    </td>
    </tr>
    </table>
    </td>
    </tr>
    </table>
    </form>
    </body>
    </HTML>代码
    Imports System.Data.SqlClient
    Imports System.io
    Public Class WebForm3
        Inherits System.Web.UI.Page#Region " Web 窗体设计器生成的代码 "    '该调用是 Web 窗体设计器所必需的。
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()    End Sub
        Protected WithEvents File1 As System.Web.UI.HtmlControls.HtmlInputFile
        Protected WithEvents File2 As System.Web.UI.HtmlControls.HtmlInputFile
        Protected WithEvents File3 As System.Web.UI.HtmlControls.HtmlInputFile
        Protected WithEvents File4 As System.Web.UI.HtmlControls.HtmlInputFile
        Protected WithEvents btnSave As System.Web.UI.WebControls.Button
        Protected WithEvents File0 As System.Web.UI.HtmlControls.HtmlInputFile
        Protected WithEvents image0 As System.Web.UI.WebControls.Image
        Protected WithEvents image1 As System.Web.UI.WebControls.Image
        Protected WithEvents image2 As System.Web.UI.WebControls.Image
        Protected WithEvents image3 As System.Web.UI.WebControls.Image    '注意: 以下占位符声明是 Web 窗体设计器所必需的。
        '不要删除或移动它。
        Private designerPlaceholderDeclaration As System.Object    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
            '不要使用代码编辑器修改它。
            InitializeComponent()
        End Sub#End Region    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            '在此处放置初始化页的用户代码
        End Sub    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
            Dim MyConn As New SqlConnection("server=lt;database=temp;uid=sa;pwd=sa")
            MyConn.Open()
            Dim mycomm As New SqlCommand("insert into pic(pic0,picType0,pic1,picType1,pic2,picType2,pic3,picType3) values (@pic0,@picType0,@pic1,@picType1,@pic2,@picType2,@pic3,@picType3)", MyConn)
            mycomm.Parameters.Add(New SqlParameter("@pic0", SqlDbType.Image))
            mycomm.Parameters.Add(New SqlParameter("@picType0", SqlDbType.VarChar, 20))
            mycomm.Parameters.Add(New SqlParameter("@pic1", SqlDbType.Image))
            mycomm.Parameters.Add(New SqlParameter("@picType1", SqlDbType.VarChar, 20))
            mycomm.Parameters.Add(New SqlParameter("@pic2", SqlDbType.Image))
            mycomm.Parameters.Add(New SqlParameter("@picType2", SqlDbType.VarChar, 20))
            mycomm.Parameters.Add(New SqlParameter("@pic3", SqlDbType.Image))
            mycomm.Parameters.Add(New SqlParameter("@picType3", SqlDbType.VarChar, 20))        Dim StrmPic As Stream
            Dim i As Integer
            For i = 0 To 3
                Dim FT As New HtmlInputFile
                FT = FindControl("File" & i)
                If FT.PostedFile.ContentLength <> 0 Then
                    Dim intPic As Int64 = FT.PostedFile.ContentLength
                    Dim strType As String = FT.PostedFile.ContentType
                    StrmPic = FT.PostedFile.InputStream
                    Dim picContent(intPic) As Byte
                    StrmPic.Read(picContent, 0, intPic)
                    mycomm.Parameters("@pic" & i.ToString()).Value = picContent
                    mycomm.Parameters("@picType" & i.ToString()).Value = strType
                Else
                    Dim fs As FileStream = New FileStream(Server.MapPath("image/NoPic.gif"), FileMode.Open, FileAccess.Read)
                    Dim br As BinaryReader = New BinaryReader(fs)
                    Dim photo() As Byte = br.ReadBytes(fs.Length)
                    Dim strType As String = "image/gif"
                    mycomm.Parameters("@pic" & i.ToString()).Value = photo
                    mycomm.Parameters("@picType" & i.ToString()).Value = strType
                    br.Close()
                    br = Nothing
                    fs.Close()
                    fs = Nothing
                End If
                FT.Dispose()
            Next
            mycomm.ExecuteNonQuery()
            mycomm.Dispose()
            Dim mycomm1 As New SqlCommand("select top 1 id from pic order by id desc", MyConn)
            Dim mydr As SqlDataReader
            mydr = mycomm1.ExecuteReader()
            If mydr.Read() Then
                image0.ImageUrl = "picShow.aspx?id=" & mydr(0) & "&num=0"
                image1.ImageUrl = "picShow.aspx?id=" & mydr(0) & "&num=1"
                image2.ImageUrl = "picShow.aspx?id=" & mydr(0) & "&num=2"
                image3.ImageUrl = "picShow.aspx?id=" & mydr(0) & "&num=3"
            End If
            mydr.Close()
            mydr = Nothing
            mycomm1.Dispose()
            MyConn.Close()
            MyConn.Dispose()    End Sub
    End Class
      

  2.   

    还忘记一个页面
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="picShow.aspx.vb" Inherits="CreateControl.picShow"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
      <head>
        <title>picShow</title>
        <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
        <meta name=vs_defaultClientScript content="JavaScript">
        <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
      </head>
      <body MS_POSITIONING="GridLayout">    <form id="Form1" method="post" runat="server">    </form>  </body>
    </html>
    -------------------------------------------------------------
    Imports System.Data
    Imports System.Data.SqlClientPublic Class picShow
        Inherits System.Web.UI.Page#Region " Web 窗体设计器生成的代码 "    '该调用是 Web 窗体设计器所必需的。
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()    End Sub    '注意: 以下占位符声明是 Web 窗体设计器所必需的。
        '不要删除或移动它。
        Private designerPlaceholderDeclaration As System.Object    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
            '不要使用代码编辑器修改它。
            InitializeComponent()
        End Sub#End Region    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            '在此处放置初始化页的用户代码
            Dim MyConn As New SqlConnection("server=lt;database=temp;uid=sa;pwd=sa")
            MyConn.Open()
            Dim strTemp As String = Request.QueryString("num")
            Dim strSql As String
            Select Case strTemp
                Case "0"
                    strSql = "select pic0,picType0 from pic where id=" & Request.QueryString("id")
                Case "1"
                    strSql = "select pic1,picType1 from pic where id=" & Request.QueryString("id")
                Case "2"
                    strSql = "select pic2,picType2 from pic where id=" & Request.QueryString("id")
                Case "3"
                    strSql = "select pic3,picType3 from pic where id=" & Request.QueryString("id")
            End Select
            Dim mycomm As New SqlCommand(strSql, MyConn)
            Dim MyDR As SqlDataReader
            MyDR = mycomm.ExecuteReader()
            If MyDR.Read() Then
                Response.ContentType = MyDR(1)
                Response.BinaryWrite(MyDR(0))
            End If
            MyDR.Close()
            MyDR = Nothing
            mycomm.Dispose()
            MyConn.Close()
            MyConn.Dispose()
        End SubEnd Class
      

  3.   

    小弟是初学者.还没有接触过ASP.NET与数据库的连接相关的知识....
    哪位大侠可以告诉我,我的那段代码是什么错误啊.谢谢....
      

  4.   

    你是上传文件到文件夹,下面是一样的            
    Dim strFileName As String = MyFile.PostedFile.FileName
                Dim strFile As String = strUserName & Year(Now()) & Month(Now()) & Day(Now()) & Hour(Now()) & Minute(Now()) & Second(Now())
                Dim I As Int16
                For I = 1 To strFileName.Length
                    If Left(Right(strFileName, I), 1) = "." Then
                        'strFileUpLoad = "\" & strFile & Right(strFileName, I)
                        strFileName = strFilePath & "/" & strFile & Right(strFileName, I)
                        Exit For
                    End If
                Next
                MyFile.PostedFile.SaveAs(strFileName.Replace("\", "/"))
                strMessage = strFileName.Replace("\", "/")
                UpLoadFile = True
    如果不行,看一下你有没有权限写入文件
      

  5.   

    strfilepath全部换成Server.MapPath("upload"),是上传到文件的路径