2011-07-30 50 views
0

我正在使用此代碼上傳圖像。我已經將寫入權限授予存儲圖像的文件夾。以下是我的代碼:顯示在GDI + whle上傳圖像中發生的一般錯誤

Dim con As New System.Data.SqlClient.SqlConnection("Data Source=Biplob-PC\SQLEXPRESS; database =a;Integrated Security=True") 

    Dim smemberid As Integer 
    Dim photoid As Integer 
    Sub bindphoto() 
     'What directory are we interested in? 
     Dim mycommand As New SqlCommand("SELECT * FROM Photo WHERE MemberID = '" & smemberid & "' ORDER BY PhotoID", con) 
     con.Open() 
     dlFileList.DataSource = mycommand.ExecuteReader 
     dlFileList.DataBind() 
     con.Close() 
    End Sub 

    Sub memberid() 
     Dim cmd As New SqlCommand("SELECT MemberID From Memberlist WHERE UserName = '" & Session("uName") & "'", con) 
     Dim r As SqlDataReader 
     con.Open() 
     r = cmd.ExecuteReader 
     If r.HasRows Then 
      r.Read() 
      smemberid = r("MemberID").ToString 
     End If 
     r.Close() 
     con.Close() 
    End Sub 

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
     If flupload.HasFile = False Then 
      Label1.Text = "Please select a picture from your computer" 
      Exit Sub 
     End If 

     If flupload.FileName.GetType.ToString = "jpg" Then 
      Label1.Text = "Hurrey" 
      Exit Sub 
     End If 

     'Has the file been uploaded properly? 
     If Not flupload.PostedFile Is Nothing Then 
      'Save the filename if it has a filename and exists... 

      Dim imageToBeResized As System.Drawing.Image = System.Drawing.Image.FromStream(flupload.PostedFile.InputStream) 

      Dim imageHeight As Integer = imageToBeResized.Height 
      Dim imageWidth As Integer = imageToBeResized.Width 

      Dim maxHeight As Integer = 98 
      Dim maxWidth As Integer = 98 

      imageHeight = (imageHeight * maxWidth)/imageWidth 
      imageWidth = maxWidth 
      Try 
       If flupload.PostedFile.FileName.Trim().Length > 0 And _ 
       flupload.PostedFile.ContentLength > 0 Then 
        photoid = (New Random).Next 
        Dim objstream As Stream = flupload.PostedFile.InputStream 
        Dim objimage As System.Drawing.Image = System.Drawing.Image.FromStream(objstream) 
        If objimage.RawFormat.Equals(ImageFormat.Gif) Or objimage.RawFormat.Equals(ImageFormat.Jpeg) Or objimage.RawFormat.Equals(ImageFormat.Png) Then 
         Dim strBaseDir As New DirectoryInfo((Request.PhysicalApplicationPath) + "images\gallery\") 


         If imageHeight > maxHeight Then 

          imageWidth = (imageWidth * maxHeight)/imageHeight 

          imageHeight = maxHeight 

         End If 


         Dim bitmap As New Bitmap(imageToBeResized, imageWidth, imageHeight) 

         Dim stream As System.IO.MemoryStream = New MemoryStream() 

         bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg) 
         stream.Position = 0 

         Dim strFileName As String = _ 
         Path.GetFileName(flupload.PostedFile.FileName) 
         bitmap.Save(((Request.PhysicalApplicationPath) + "images\gallery\thumbs\") & photoid & ".jpg") 
         bigimage() 
         'File has been saved! 
         Dim mycommand As New SqlCommand("Insert chairperson (memberid,name,period,achieve,imageurl,other) Values ('" & smemberid & "','" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & photoid & "','" & TextBox4.Text & "')", con) 
         con.Open() 
         mycommand.ExecuteNonQuery() 
         con.Close() 

         Label1.Text = "File has been successfully uploaded" 


        Else 
         Label1.Text = "Sorry, File format not supported." 
        End If 

       End If 
      Catch ex As Exception 
       Label1.Text = ex.Message 
      End Try 
     Else 
      Label1.Text = "<hr /><p>Enter a filename to upload!" 
     End If 

    End Sub 
    Sub bigimage() 
     Dim imageToBeResized As System.Drawing.Image = System.Drawing.Image.FromStream(flupload.PostedFile.InputStream) 

     Dim imageHeight As Integer = imageToBeResized.Height 
     Dim imageWidth As Integer = imageToBeResized.Width 

     Dim maxHeight As Integer = 450 
     Dim maxWidth As Integer = 450 

     imageHeight = (imageHeight * maxWidth)/imageWidth 
     imageWidth = maxWidth 
     Dim objstream As Stream = flupload.PostedFile.InputStream 
     Dim objimage As System.Drawing.Image = System.Drawing.Image.FromStream(objstream) 
     If objimage.RawFormat.Equals(ImageFormat.Gif) Or objimage.RawFormat.Equals(ImageFormat.Jpeg) Or objimage.RawFormat.Equals(ImageFormat.Png) Then 
      Dim strBaseDir As New DirectoryInfo((Request.PhysicalApplicationPath) + "images\gallery\") 


      If imageHeight > maxHeight Then 

       imageWidth = (imageWidth * maxHeight)/imageHeight 

       imageHeight = maxHeight 

      End If 

      Dim bitmap As New Bitmap(imageToBeResized, imageWidth, imageHeight) 

      Dim stream As System.IO.MemoryStream = New MemoryStream() 

      bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg) 

      stream.Position = 0 

      Dim strFileName As String = _ 
      Path.GetFileName(flupload.PostedFile.FileName) 
      bitmap.Save(((Request.PhysicalApplicationPath) + "images\gallery\") & photoid & ".jpg") 

     End If 
    End Sub 
    Sub deleteg(ByVal s As Object, ByVal f As DataListCommandEventArgs) 
     Dim photographid As String 
     photographid = dlFileList.DataKeys.Item(f.Item.ItemIndex).ToString 
     Dim mycommand As New SqlCommand("DELETE FROM Photo WHERE PhotoID = '" & photographid & "'", con) 
     con.Open() 
     mycommand.ExecuteNonQuery() 
     con.Close() 
     bindphoto() 
     Label1.Text = "File has been deleted succefully" 
    End Sub 

回答

0

什麼意思是通用錯誤?

在任何情況下,我看到一對夫婦的問題以您的按鈕單擊事件:

If flupload.FileName.GetType.ToString = "jpg" Then 
    Label1.Text = "Hurrey" 
    Exit Sub 
End If 

聲明flupload.FileName.GetType.toString = "jpg"甚至不應該編譯。 GetType()返回對象的類型(在本例中爲System.String)。

如果它甚至沒有編譯,那麼語句總是會失敗,無論FileName是什麼。我想你要找的東西是這樣的:

If flupload.FileName.EndsWith("jpg") Then 

我不知道爲什麼你會在所有你是在按鈕單擊事件處理程序的其餘部分的排列,作爲保存文件應該像撥打flupload.SaveAs(<path>)一樣簡單。

此外,你暴露你的應用程序與下面的代碼SQL注入攻擊:

Dim mycommand As New SqlCommand("Insert chairperson (memberid,name,period,achieve,imageurl,other) Values ('" & smemberid & "','" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & photoid & "','" & TextBox4.Text & "')", con) 

您應該使用參數化查詢。

閱讀(徹底)FileUpload Class - 它擁有您開始所需的一切,並且還有示例代碼。

0

這個錯誤可能是由很多事情造成的。

我得到了這個錯誤信息幾次,每次都是因爲權限問題,或者因爲我發現了一個文件夾或正在寫入的文件夾的拼寫錯誤而無法找到。確保子目錄也可以寫入,並且正確的進程可以訪問該文件夾。如果您調試代碼發生異常的位置?

你可能也想看看A generic error occurred in GDI+其他的東西來看看。

相關問題