vb.net 使用 Visual Studio 中的 FileUpload 工具上传图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16106982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uploading an image using FileUpload tool within Visual Studio
提问by Steven Trainor
I am creating a content management system(CMS) and I have been asked to include image uploading, the requirements for the image are below:
我正在创建一个内容管理系统(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
So far i have the following code:
到目前为止,我有以下代码:
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
Has anyone any idea how i would go about this?
有谁知道我会怎么做?
回答by Kenneth
The 150K => that means 150 KiloByte. The content-length is set in bytes so that would be (150 * 1024) => 153600
150K => 表示 150 KB。内容长度以字节为单位设置,因此将是 (150 * 1024) => 153600
As for the dimensions of the Image you could do this:
至于图像的尺寸,你可以这样做:
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
After that you check its dimensions
之后你检查它的尺寸

