2010-10-18 6 views
2

Je faisais un test pour télécharger des photos et j'ai découvert que lorsque je télécharge des images de plus de 2000px la page web devient lente. Je veux que les utilisateurs téléchargent des images avec la taille pas plus de 600px et la taille 700px.télécharger des photos jpg/png/gif

Imports System.Data Imports System.IO Imports System.Data.SqlClient

classe partielle PhotoAdmin_Default Hérite 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 

End Class

Répondre

1

Désolé si mon vb est erroné comme im ac guy #! ! mais j'espère que cela devrait vous guider. Je fais quelque chose de similaire en C#. Bonne chance

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 
+0

Merci beaucoup ma question est maintenant répondu. En ce moment je peux seulement ajouter jpg, pouvez-vous m'aider à ajouter l'extension de fichier gif et png au code? – onfire4JesusCollins

2

Réglage une largeur et une hauteur maximales sur une image téléchargée ne vont pas nécessairement résoudre votre problème car vous pourriez télécharger une image avec un DPI beaucoup plus élevé et ce sera toujours une "grande" image avec sma ll dimensions. En outre, vous devrez vérifier les dimensions de l'image une fois l'image téléchargée sur votre serveur.

Vous pouvez définir une taille de fichier maxiumum dans le web.config en utilisant la propriété maxRequestLength qui pourrait être utilisé pour éviter un gros fichier en cours de téléchargement ...

Questions connexes