用FileStream上傳文件轉換成二進制

 FileStream上傳圖片轉換成二進制,在本地用行,傳到服務器上去出現如下錯誤

未能找到路徑“C:/Documents and Settings/xxxxxxx.doc”的一部分。
說明: 執行當前 Web 請求期間,出現未處理的異常。請檢查堆棧跟蹤信息,以瞭解有關該錯誤以及代碼中導致錯誤的出處的詳細信息。

異常詳細信息: System.IO.DirectoryNotFoundException: 未能找到路徑“C:/Documents and Settings/ xxxxxxx.doc”的一部分。

Debug,本地上傳沒有任何問題。

分析:根據提示“找不到本地路徑…”,不是權限的問題。

查閱相關資料,得到出錯的結論如下:

1.      程序運行在服務器端,上傳文件來自於客戶端。

2.      FileStream 只支持本地數據流上傳,所有會造成本地上傳沒有問題,上傳到服務器時報錯。

3.      Stream允許遠程數據流上傳,本地到服務器,本地到本地都沒有問題。

 

修改:將FileStream改成Stream

 

將代碼:

' Dim fs As New FileStream(xHIF.PostedFile.FileName, FileMode.Open, FileAccess.Read)

' Dim br As New BinaryReader(fs)

 

修改爲:

Dim fs As Stream = xHIF.PostedFile.InputStream

Dim br As New BinaryReader(fs)

Dim bytes As Byte() = br.ReadBytes(CInt(fs.Length))

br.Close()

fs.Close()

 

 

完整上傳文件代碼片斷

 

Protected Sub Button_File_Save_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button_Plans_Save.Click

            Dim baseLocation = "D:/CAR_Files/"

            Try

 

                Dim status = ""

                Dim istg_plan_id As String

                istg_plan_id = GetPlansID()

 

                If (ListBox_Files.Items.Count = 0) And (filesUploaded = 0) Then

                    Me.Label_File_Status.Text = "Error - a file name must be specified."

                    Return

                Else

                    If Me.CreateCar3DPlans(istg_plan_id) = True Then

                        For Each xHIF As System.Web.UI.HtmlControls.HtmlInputFile In hif

 

                            Dim fn As String = System.IO.Path.GetFileName(xHIF.PostedFile.FileName)

                            Dim l_fn As String

 

                            xHIF.PostedFile.SaveAs(baseLocation & fn)

                            filesUploaded = filesUploaded + 1

                            status = status & fn & "<br>"

 

                            l_fn = baseLocation & fn

 

                            ' Dim fs As New FileStream(xHIF.PostedFile.FileName, FileMode.Open, FileAccess.Read)

                            ' Dim br As New BinaryReader(fs)

                            Dim fs As Stream = xHIF.PostedFile.InputStream

                            Dim br As New BinaryReader(fs)

                            Dim bytes As Byte() = br.ReadBytes(CInt(fs.Length))

                            br.Close()

                            fs.Close()

 

 

                            Dim lf_desc As String

                            Dim lf_type As String

                            Dim lf_size As String

 

                            lf_desc = System.IO.Path.GetExtension(xHIF.PostedFile.FileName)

                            lf_type = xHIF.PostedFile.ContentType.ToString

                            lf_size = xHIF.PostedFile.ContentLength.ToString

 

 

                            CreateCar3DPlansFile(istg_plan_id, fn, lf_desc, lf_type, lf_size, bytes, l_fn)

 

                            Response.Write(fn)

 

                        Next

 

                    End If

 

                    If filesUploaded = hif.Count Then

                        Me.Label_File_Status.Text = "These " & filesUploaded.ToString & " file(s) were uploaded:<br>" + status

                    End If

                    hif.Clear()

                    ListBox_Files.Items.Clear()

 

                End If

                GetCar3DPlans()

            Catch ex As Exception

                Response.Write(ex.ToString)

                Me.Label_File_Status.Text = "Error saving file " & baseLocation & "<br>" & Err.ToString()

            End Try

        End Sub

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章