0

我正在创建一个内容管理系统(CMS),我被要求包括图片上传,图片的要求如下:

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

有谁知道我会怎么做?

4

1 回答 1

1

150K => 表示 150 KiloByte。内容长度以字节为单位设置,因此将是 (150 * 1024) => 153600

至于图像的尺寸,您可以这样做:

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

之后你检查它的尺寸

于 2013-04-19T14:53:12.830 回答