2010-01-27 52 views
0

重新發布相同的問題,因爲發佈後無法編輯問題。請特別閱讀下面的內容,如果你之前閱讀過這個問題 -雖然vb.net上傳圖片文件在sql server上被卡住

好的,所以這是我從這個網站使用的代碼。一切工作正常,但我需要一點點不同的代碼上傳圖片,我不知道該怎麼辦 - 這裏的代碼 -

Private Sub btnAttach_Click(ByVal sender As System.Object, _ 
ByVal e As System.EventArgs) Handles btnAttach.Click 
    Dim iLength As Integer = CType(File1.PostedFile.InputStream.Length, Integer) 
    If iLength = 0 Then Exit Sub 'not a valid file 
    Dim sContentType As String = File1.PostedFile.ContentType 
    Dim sFileName As String, i As Integer 
    Dim bytContent As Byte() 
    ReDim bytContent(iLength) 'byte array, set to file size 

'strip the path off the filename ' 
i = InStrRev(File1.PostedFile.FileName.Trim, "\") 
If i = 0 Then 
    sFileName = File1.PostedFile.FileName.Trim 
Else 
    sFileName = Right(File1.PostedFile.FileName.Trim, Len(File1.PostedFile.FileName.Trim) - i) 
End If 

Try 
    File1.PostedFile.InputStream.Read(bytContent, 0, iLength) 
    With cmdInsertAttachment 
     .Parameters("@FileName").Value = sFileName 
     .Parameters("@FileSize").Value = iLength 
     .Parameters("@FileData").Value = bytContent 
     .Parameters("@ContentType").Value = sContentType 
     .ExecuteNonQuery() 
    End With 
Catch ex As Exception 
    'Handle your database error here 
    dbConn.Close() 
End Try 
Response.Redirect(Request.Url.ToString) 'Refresh page 
End Sub 

一切正常,當涉及到這部分除了 -

With cmdInsertAttachment 
    .Parameters("@FileName").Value = sFileName 
    .Parameters("@FileSize").Value = iLength 
    .Parameters("@FileData").Value = bytContent 
    .Parameters("@ContentType").Value = sContentType 
    .ExecuteNonQuery() 
End With 

我沒有使用cmdinsertattachment。我使用HTML工具箱中的Html - Input(File)。輸入文件的ID是ID =「upldimg」。

讓我怎麼插入我的表,該表是這樣的 -

Column1 ID identity 
Column2 Img image 
Column 3 Description varchar(200). 

,請告訴我,像插入語句 -

INSERT into table1 (Img, Description) values (???, txtdescription.text) 

辦upldimg.text在INSERT語句?

+0

因爲你的身份登錄derti代替drien你不能編輯的問題(見http://stackoverflow.com /問題/ 2150251 /卡住,而上載-的圖像文件,在-SQL服務器2008年從-VB淨代碼) – ErikE 2010-01-29 01:21:26

回答

0
  1. 使用適當的數據類型。 IMAGE已棄用,請使用VARBINARY(MAX)
  2. 不要爲任意圖像大小分配字節數組。使用塊。
  3. 請勿阻止等待數據庫上傳整個文件的響應。

如果我這樣做,我會用IIS模塊來處理正在接收HTTP請求而文件的分塊,以完全避免創建臨時文件。艾利簡單的方法是這樣的:

create table Uploads (
    Id int identity(1,1) primary key 
    , FileName varchar(256) 
    , ContentType varchar(256) 
    , FileData varbinary(max)); 

和ASPX:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (IsPostBack && FileUpload1.HasFile) 
     { 
      ThreadPool.QueueUserWorkItem(
       new WaitCallback(TransferToDatabase), FileUpload1); 
     } 
    } 

    protected void TransferToDatabase(object args) 
    { 
     try 
     { 
      Debug.Assert(args is FileUpload); 
      FileUpload upload = (FileUpload)args; 
      using (SqlConnection conn = new SqlConnection(
       Settings.Default.connString)) 
      { 
       conn.Open(); 
       using (SqlTransaction trn = conn.BeginTransaction()) 
       { 
        SqlCommand cmd = new SqlCommand(@" 
INSERT INTO Uploads(FileName, ContentType, FileData) 
VALUES (@FileName, @ContentType, @initialChunk); 
SET @id = SCOPE_IDENTITY();", conn, trn); 
        cmd.Parameters.AddWithValue("@FileName", upload.PostedFile.FileName); 
        cmd.Parameters.AddWithValue("@contentType", upload.PostedFile.ContentType); 
        SqlParameter paramId = new SqlParameter("@id", SqlDbType.Int); 
        paramId.Direction = ParameterDirection.Output; 
        cmd.Parameters.Add(paramId); 

        byte[] chunk = new byte[4096]; 
        int offset = upload.FileContent.Read(chunk, 0, 4096); 
        byte[] initialChunk = chunk; 
        if (offset < 4096) 
        { 
         // can't pass only part of a byte[] as parameter value 
         // must copy out the appropiate size 
         initialChunk = new byte[offset]; 
         Array.Copy(chunk, initialChunk, offset); 
        } 
        cmd.Parameters.AddWithValue("@initialChunk", initialChunk); 
        cmd.ExecuteNonQuery(); 

        // Add the rest of the data 
        if (offset == 4096) 
        { 
         SqlParameter paramChunk = new SqlParameter("@chunk", SqlDbType.VarBinary, 4096); 
         SqlParameter paramLength = new SqlParameter("@length", SqlDbType.BigInt); 
         SqlCommand cmdAddChunk = new SqlCommand(@" 
UPDATE Uploads 
    SET FileData.Write(@chunk, NULL, @length) 
WHERE id = @id", conn, trn); 
         cmdAddChunk.Parameters.AddWithValue("@id", paramId.Value); 
         cmdAddChunk.Parameters.Add(paramChunk); 
         cmdAddChunk.Parameters.Add(paramLength); 

         do 
         { 
          int chunkSize = upload.FileContent.Read(chunk, 0, 4096); 
          if (0 == chunkSize) 
          { 
           break; 
          } 
          paramChunk.Value = chunk; 
          paramLength.Value = chunkSize; 

          cmdAddChunk.ExecuteNonQuery(); 

          offset += chunkSize; 

         } while (true); 
        } 

        trn.Commit(); 
       } 
      } 
     } 
     catch (Exception e) 
     { 
      // Log to the appropiate error logging infrastructure 
      Debug.Write(e); 
     } 
    }