gpt4 book ai didi

asp.net - 上传jpg/png/gif照片

转载 作者:行者123 更新时间:2023-12-04 06:43:30 26 4
gpt4 key购买 nike

我正在做一个上传图片的测试,我发现当我上传超过2000px的图片时,网页变得很慢。我希望用户上传大小不超过 600 像素和高度 700 像素的图片。

导入 System.Data
进口系统.IO
导入 System.Data.SqlClient

部分类 PhotoAdmin_Default
继承 System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
UserIdValue.Text = Membership.GetUser().ProviderUserKey.ToString()
cannotUploadImageMessage.Visible = False

End Sub


Protected Sub dvPictureInsert_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles dvPictureInsert.ItemInserted
'If the record was successfully inserted, save the picture
If e.AffectedRows > 0 Then
'Determine the maximum pictureID for this user
Dim results As DataView =
CType(maxPictureIDDataSource.Select(DataSourceSelectArguments.Empty),
DataView)

Dim pictureIDJustAdded As Integer = CType(results(0)(0), Integer)

'Reference the FileUpload control
Dim imageUpload As FileUpload =
CType(dvPictureInsert.FindControl("imageUpload"), FileUpload)

If imageUpload.HasFile Then
Dim baseDirectory As String = Server.MapPath("~/UploadedImages/")

imageUpload.SaveAs(baseDirectory & pictureIDJustAdded & ".jpg")
End If

End If

If e.Exception Is Nothing Then

' Use the AffectedRows property to determine whether the
' record was inserted. Sometimes an error might occur that
' does not raise an exception, but prevents the insert
' operation from completing.
If e.AffectedRows = 1 Then

MessageLabel.Text = "Record inserted successfully."

Else

MessageLabel.Text = "An error occurred during the insert operation."

' Use the KeepInInsertMode property to remain in insert mode
' when an error occurs during the insert operation.
e.KeepInInsertMode = True

End If

Else

' Insert the code to handle the exception.
MessageLabel.Text = e.Exception.Message

' Use the ExceptionHandled property to indicate that the
' exception has already been handled.
e.ExceptionHandled = True
e.KeepInInsertMode = True

End If

End Sub



Protected Sub dvPictureInsert_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles dvPictureInsert.ItemInserting
Dim cancelInsert As Boolean = False


Dim imageUpload As FileUpload =
CType(dvPictureInsert.FindControl("imageUpload"), FileUpload)

If Not imageUpload.HasFile Then
cancelInsert = True
Else
If Not imageUpload.FileName.ToUpper().EndsWith(".JPG") Then
cancelInsert = True 'Invalid image file!

End If
End If

If cancelInsert Then
e.Cancel = True
cannotUploadImageMessage.Visible = True
End If

'Set the UserId value to the currently logged on user's ID
e.Values("UserId") = Membership.GetUser().ProviderUserKey

'Set the UploadedOn value to the current date/time
e.Values("UploadedOn") = DateTime.Now
End Sub

Protected Sub gvPictures_RowDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeletedEventArgs) Handles gvPictures.RowDeleted
Dim baseDirectory As String = Server.MapPath("~/UploadedImages/")
Dim fileName As String = baseDirectory &
e.Keys("PictureID") & ".jpg"
File.Delete(fileName)
End Sub

Protected Sub gvPictures_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvPictures.RowUpdating
e.NewValues("UserId") = Membership.GetUser().ProviderUserKey
End Sub

结束类

最佳答案

对不起,如果我的 vb 是错误的,因为我是一个 c# 人!!但我希望这能给你一个指导。我在 c# 中做了类似的事情。祝你好运

Protected Sub dvPictureInsert_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles dvPictureInsert.ItemInserting
Dim cancelInsert As Boolean = False


Dim imageUpload As FileUpload =
CType(dvPictureInsert.FindControl("imageUpload"), FileUpload)

If Not imageUpload.HasFile Then
cancelInsert = True
Else
If Not imageUpload.FileName.ToUpper().EndsWith(".JPG") Then
cancelInsert = True 'Invalid image file!
Else
Dim image As System.Drawing.Image =
System.Drawing.Image.FromStream(imageUpload.PostedFile.InputStream)
If image.Width > 600 Or image.Height > 700 Then
cancelInsert = True
End If
End If
End If

//etc

关于asp.net - 上传jpg/png/gif照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3956355/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com