2013-04-19 4 views
0

나는 콘텐츠 관리 시스템 (CMS)을 만드는 오전 내에는 FileUpload 도구를 사용하여 이미지를 업로드 및 I 이미지 업로드를 포함하도록 요청을받은, 이미지의 요구 사항은 다음과 같습니다 :비주얼 스튜디오

Check that the image in JPEG or PNG format. 
Check to see that an image with the same filename isn’t already stored on your site. 
Check that the image file is under 150K in size and the image is no greater than 400 X 400 pixels (if not the image must be resized to that specification). 
The image must be stored in a folder and not in the database itself 
The image must referenced in a relevant database table 

지금까지 내가 다음 코드를 사용하십시오.

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click 
    Dim filename As String 

    Dim img As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath("~/Images/Property/") + filename) 
    Dim width As Integer = img.Size.Width 
    Dim height As Integer = img.Size.Height 
    If PhotoUploadControl.PostedFile.ContentLength < 153600 Then 
     If PhotoUploadControl.HasFile Then 
      If PhotoUploadControl.PostedFile.ContentType = "image/jpeg" Then 
       If PhotoUploadControl.PostedFile.ContentLength < 153600 Then 
        Try 
         filename = Path.GetFileName(PhotoUploadControl.FileName) 
         PhotoUploadControl.SaveAs(Server.MapPath("~/Images/Property/") + filename) 
         StatusLabel.Text = "Upload status: File uploaded!" 
        Catch ex As Exception 
         StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message 
        End Try 
       Else 
        StatusLabel.Text = "Please upload an image" 
       End If 
      Else 
       StatusLabel.Text = "Please upload an image less than 150k" 
      End If 
     End If 
    End If 

End Sub 

나는이 문제에 대해 어떻게 생각합니까?

+0

거의 다 왔으니 문제가 뭐니? – Kenneth

+0

내가 여기에서 멈췄다 - 이미지 파일의 크기가 150K 미만이고 이미지의 크기가 400x400 픽셀보다 크지 않은지 확인하십시오 (이미지가 해당 사양으로 리사이즈되어야하는 경우). 또한 <102400에서 150k까지 어떻게 변경합니까? –

답변

1

150K = 150KiloByte를 의미합니다. 내용 길이는 그 것 때문에 바이트 단위로 설정 (1024 * 150)이 할 수있는 이미지의 크기에 관해서는 => 153,600

: 당신은 그것의 크기를 확인 그 후

Dim img as System.Drawing.Image= System.Drawing.Image.FromFile(Server.MapPath("~/Images/NewAdmin/") + filename) 

Dim width as Integer = img.Size.Width 
Dim height as Integer = img.Size.Height 

+0

다음과 같이 확인하십시오. - img> 153600 또는 <153600 인 경우 img = 153600 end if? –

+0

네, 그렇지만 정확히 150K 인 이미지를 업로드하라고 사용자에게 묻습니다. 단지 바보 같기 때문에 'PhotoUploadControl.PostedFile.ContentLength <153600'은 (는) 트릭을 수행 할 것입니다 – Kenneth

+0

마지막으로 의견, 감사합니다. 그러나 Image.FromFile 및 img.Size에서 오류가 발생합니다. 'FromFile'오류는 'System.Web.UI.WebControls.Image'의 구성원이 아닙니다./ –