Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    modificare upload trovato in rete

    Ciao,

    qualcuno può aiutarmi nell'impresa in oggetto????

    Posto il codice:

    Codice PHP:

    <%@ Page Trace="False" Language="vb" aspcompat="false" debug="true" validateRequest="false"%> 
    <%@ 
    Import Namespace=System.Drawing %>
    <%@ 
    Import Namespace=System.Drawing.Imaging %>
    <%@ 
    Import Namespace=System %>
    <%@ 
    Import Namespace=System.Web %>
    <
    SCRIPT LANGUAGE="VBScript" runat="server">
    const 
    Lx 200    ' max width for thumbnails
    const Ly = 200    ' 
    max height for thumbnails
    const upload_dir "/images/"    ' directory to upload file
    const upload_original = "sample" ' 
    filename to save original as (suffix added by script)
    const 
    upload_thumb "thumb" ' filename to save thumbnail as (suffix added by script)
    const upload_max_size = 1024 ' 
    max size of the upload (KBnotethis doesn't override any server upload limits
    dim fileExt    ' 
    used to store the file extension (saves finding it mulitple times)
    dim newWidthnewHeight as integer ' new width/height for the thumbnail
    dim l2    ' 
    temp variable used when calculating new size
    dim fileFld 
    as HTTPPostedFile    ' used to grab the file upload from the form
    Dim originalimg As System.Drawing.Image    ' 
    used to hold the original image
    dim msg    
    ' display results
    dim upload_ok as boolean    ' 
    did the upload work ?
    </script>
    <%
    randomize() ' used to help the cache-busting on the preview images
    upload_ok = false
    if lcase(Request.ServerVariables("REQUEST_METHOD"))="post" then


        fileFld = request.files(0)    ' get the first file uploaded from the form (note:- you can use this to itterate through more than one image)
        if fileFld.ContentLength > upload_max_size * 1024 then
            msg = "Sorry, the image must be less than " & upload_max_size & "Kb"
        else
            try
                originalImg = System.Drawing.Image.FromStream(fileFld.InputStream)
                ' work out the width/height for the thumbnail. Preserve aspect ratio and honour max width/height
                ' Note: if the original is smaller than the thumbnail size it will be scaled up
                If (originalImg.Width/Lx) > (originalImg.Width/Ly) Then
                    L2 = originalImg.Width
                    newWidth = Lx
                    newHeight = originalImg.Height * (Lx / L2)
                    if newHeight > Ly then
                        newWidth = newWidth * (Ly / newHeight)
                        newHeight = Ly
                    end if
                Else
                    L2 = originalImg.Height
                    newHeight = Ly
                    newWidth = originalImg.Width * (Ly / L2)
                    if newWidth > Lx then
                        newHeight = newHeight * (Lx / newWidth)
                        newWidth = Lx
                    end if
                End If

                Dim thumb As New Bitmap(newWidth, newHeight)

                'Create a graphics object           
                Dim gr_dest As Graphics = Graphics.FromImage(thumb)

                ' just in case it's a transparent GIF force the bg to white 
                dim sb = new SolidBrush(System.Drawing.Color.White)
                gr_dest.FillRectangle(sb, 0, 0, thumb.Width, thumb.Height)

                'Re-draw the image to the specified height and width
                gr_dest.DrawImage(originalImg, 0, 0, thumb.Width, thumb.Height)

                try
                    fileExt = System.IO.Path.GetExtension(fileFld.FileName).ToLower()
                     originalImg.save(Server.MapPath(upload_dir & upload_original & fileExt), originalImg.rawformat)
                    thumb.save(Server.MapPath(upload_dir & upload_thumb & fileExt), originalImg.rawformat)
                    msg = "Uploaded " & fileFld.FileName & " to " & Server.MapPath(upload_dir & upload_original & fileExt)
                    upload_ok = true
                catch
                    msg = "Sorry, there was a problem saving the image."
                end try
                ' Housekeeping for the generated thumbnail
                if not thumb is nothing then
                    thumb.Dispose()
                    thumb = nothing
                end if
            catch
                msg = "Sorry, that was not an image we could process."
            end try
        end if

        ' House Keeping !
        if not originalImg is nothing then
            originalImg.Dispose()
            originalImg = nothing
        end if
        

    end if
    %>
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <form enctype="multipart/form-data" method="post" runat="server">
      <table align="center"class="table">
      <tr>
        <td class="table_header" colspan="2">[b]ASP.NET File Upload and Resize[/b]</td>
      </tr>
      <tr>
        <td colspan="2" class="upload_info">[b]Allowed Types:[/b] GIF, JPG, PNG

            [b]Max size per file:[/b]
            <%=upload_max_size%>      kb.

        </td>
      </tr>

      <tr>
        <td class="table_body" width="20%">[b]Select File:[/b] </td>
        <td class="table_body" width="80%"><input name="upload_file" type="file" id="upload_file" size="30" /></td>
      </tr>
      <tr>
        <td colspan="2" align="center" class="table_footer"><input type="submit" value=" Upload File" />
          
          <input type="reset" name="reset" value=" Reset Form " /> 
          </td>
      </tr>
    </table>
    </form>
    <table class="table" style="border:0px;" align="center">
      <tr>
        <td><div class="copyright"></div></td>
      </tr>
    </table>
    <%
    if upload_ok then
    %>
    <table>
    <tr>
    <td valign=top>[img]<%=upload_dir & upload_original & fileExt & [/img]"></td>
    <td valign=top>[img]<%=upload_dir & upload_thumb & fileExt & [/img]"></td>
    </tr>
    </table>
    <%
    else
        response.write(msg)
    end if
    %>


    </p>
    </body>
    </html> 
    Lo script funziona molto bene...

    Cosa vorrei?
    Per prima cosa poter uploadare più immagini alla volta, quindi come si legge in una nota:

    fileFld = request.files(0) ' get the first file uploaded from the form (note:- you can use this to itterate through more than one image)

    Ovviamente creare i thumb per tutte le immagini uppate... si può fare? Come? Purtroppo conosco un po' di ASP ma non ASP.Net, appena provo a modificare qualcosa ottengo errore!!!
    "Quando si mangia il formaggio svizzero, che succede ai buchi?" (B. Brecht)

    Visitate il mio sito:
    www.clamorosalcibali.it

  2. #2
    quanta collaborazione
    "Quando si mangia il formaggio svizzero, che succede ai buchi?" (B. Brecht)

    Visitate il mio sito:
    www.clamorosalcibali.it

  3. #3
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    Originariamente inviato da Kai Hansen
    quanta collaborazione
    Ci vuole coraggio a lamentarsi codice di tal fatta è un pezzo che non ne vedo

    comunque, prova questo:
    codice:
    <%@ Page Trace="False" Language="vb" AspCompat="false" Debug="true" ValidateRequest="false" Strict="false" %>
    
    <%@ Import Namespace="System.Drawing" %>
    <%@ Import Namespace="System.Drawing.Imaging" %>
    <%@ Import Namespace="System" %>
    <%@ Import Namespace="System.Web" %>
    
    <script language="VBScript" runat="server">
        Const Lx = 200  ' max width for thumbnails
        Const Ly = 200  ' max height for thumbnails
        Const upload_dir = "/tmp/"   ' directory to upload file
        Dim upload_original = "sample" ' filename to save original as (suffix added by script)
        Dim upload_thumb = "thumb" ' filename to save thumbnail as (suffix added by script)
        Const upload_max_size = 1024 ' max size of the upload (KB) note: this doesn't override any server upload limits
        Dim fileExt ' used to store the file extension (saves finding it mulitple times)
        Dim newWidth, newHeight As Integer ' new width/height for the thumbnail
        Dim l2  ' temp variable used when calculating new size
        Dim fileFld As HttpPostedFile   ' used to grab the file upload from the form
        Dim originalimg As System.Drawing.Image ' used to hold the original image
        Dim msg ' display results
        Dim upload_ok As Boolean    ' did the upload work ?
        Dim n = -1
    </script>
    
    <%
        Randomize() ' used to help the cache-busting on the preview images
        upload_ok = False
        If LCase(Request.ServerVariables("REQUEST_METHOD")) = "post" Then
            For i = 0 To Request.Files.Count - 1
                fileFld = Request.Files(i)  ' get the first file uploaded from the form (note:- you can use this to itterate through more than one image)
                If fileFld.ContentLength > upload_max_size * 1024 Then
                    msg = "Sorry, the image must be less than " & upload_max_size & "Kb"
                Else
                    Try
                        originalimg = System.Drawing.Image.FromStream(fileFld.InputStream)
                        ' work out the width/height for the thumbnail. Preserve aspect ratio and honour max width/height
                        ' Note: if the original is smaller than the thumbnail size it will be scaled up
                        If (originalimg.Width / Lx) > (originalimg.Width / Ly) Then
                            l2 = originalimg.Width
                            newWidth = Lx
                            newHeight = originalimg.Height * (Lx / l2)
                            If newHeight > Ly Then
                                newWidth = newWidth * (Ly / newHeight)
                                newHeight = Ly
                            End If
                        Else
                            l2 = originalimg.Height
                            newHeight = Ly
                            newWidth = originalimg.Width * (Ly / l2)
                            If newWidth > Lx Then
                                newHeight = newHeight * (Lx / newWidth)
                                newWidth = Lx
                            End If
                        End If
    
                        Dim thumb As New Bitmap(newWidth, newHeight)
    
                        'Create a graphics object           
                        Dim gr_dest As Graphics = Graphics.FromImage(thumb)
    
                        ' just in case it's a transparent GIF force the bg to white 
                        Dim sb = New SolidBrush(System.Drawing.Color.White)
                        gr_dest.FillRectangle(sb, 0, 0, thumb.Width, thumb.Height)
    
                        'Re-draw the image to the specified height and width
                        gr_dest.DrawImage(originalimg, 0, 0, thumb.Width, thumb.Height)
    
                        Try
                            fileExt = System.IO.Path.GetExtension(fileFld.FileName).ToLower()
                            originalimg.Save(Server.MapPath(upload_dir & upload_original & "_" & i & fileExt), originalimg.RawFormat)
                            thumb.Save(Server.MapPath(upload_dir & upload_thumb & "_" & i & fileExt), originalimg.RawFormat)
                            msg = "Uploaded " & fileFld.FileName & " to " & Server.MapPath(upload_dir & upload_original & fileExt)
                            upload_ok = True
                        Catch
                            msg = "Sorry, there was a problem saving the image."
                        End Try
                        ' Housekeeping for the generated thumbnail
                        If Not thumb Is Nothing Then
                            thumb.Dispose()
                            thumb = Nothing
                        End If
                        n += 1
                        
                    Catch
                        msg = "Sorry, that was not an image we could process."
                    End Try
                End If
                
            Next
    
            ' House Keeping !
            If Not originalimg Is Nothing Then
                originalimg.Dispose()
                originalimg = Nothing
            End If
    	
    
        End If
    %>
    <html>
    <head>
        <title></title>
    
    <script language="javascript" type="text/javascript">
    // <!CDATA[
    var n = 0;
    function aggiungi_riga()
    {
        n++;
        var tabella = document.getElementById("table1");
        if(!tabella) return;
        var tb = tabella.getElementsByTagName("tbody").item(0);
        var riga = document.createElement("tr");
        var colonna = document.createElement("td");
        colonna.innerHTML = "Select File:";
        riga.appendChild(colonna);
        colonna = document.createElement("td");
        colonna.innerHTML = '<input id="upload_file_' + n + '" name="upload_file_' + n + '" type="file" size="30" />';
        riga.appendChild(colonna);
        colonna = document.createElement("td");
        colonna.innerHTML = '';
        riga.appendChild(colonna);
        tb.appendChild(riga);
        
        
        
    }
    
    // ]]>
    </script>
    
    </head>
    <body>
        <form id="Form1" enctype="multipart/form-data" method="post" runat="server">
        <table id="table1" align="center" class="table">
            <thead>
                <tr>
                    <td class="table_header" colspan="3">
                        ASP.NET File Upload and Resize
                    </td>
                </tr>
                <tr>
                    <td colspan="3" class="upload_info">
                        Allowed Types: GIF, JPG, PNG
    
                        Max size per file:
                        <%=upload_max_size%>
                        kb.
    
                    </td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td class="table_body" >
                        Select File:
                    </td>
                    <td class="table_body" >
                        <input id="upload_file_0" name="upload_file_0" type="file" size="30" />
                    </td>
                    <td>
                        Aggiungi riga
                    </td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="3" align="center" class="table_footer">
                        <input type="submit" value=" Upload File" />
                        
                        <input type="reset" name="reset" value=" Reset Form " />
                        
                    </td>
                </tr>
            </tfoot>
        </table>
        </form>
        <table class="table" style="border: 0px;" align="center">
            <tr>
                <td>
                    <div class="copyright">
                    </div>
                </td>
            </tr>
        </table>
        <%
            If upload_ok Then
        %>
        
        <table>
        
            <%For i = 0 To n%>
            <tr>
                <td valign="top">
                    [img]<%=upload_dir & upload_original & [/img]">
                </td>
                <td valign="top">
                    [img]<%=upload_dir & upload_thumb & [/img]">
                </td>
            </tr>
            <%Next%>
        </table>
        <%
        Else
            Response.Write(msg)
        End If
        %>
        
    
    
            </p>
    </body>
    </html>
    Pietro

  4. #4
    Gentilissimo pietro09,

    non mi volevo lamentare... mi serviva una "scusa" per fare l'up della discussione, visto che nessuno in 1 giorno aveva provato ad aiutarmi... e per questo ti ringrazio davvero!!!

    Purtroppo il codice, come dicevo, l'ho trovato in rete... non conoscendo l'ASP.NET non so valutare se sia un buon codice o una porcheria... ma dalla tua affermazione, capisco che non è il massimo... ma se tu hai da consigliarmi qualcosa di meglio... un upload multiplo con la creazione dei thumb... mi faresti felice...

    Il mio piccolo sogno sarebbe riuscire a utilizzare swfupload (http://swfupload.org/) ma non so proprio come far interaggire il swfupload con la pagina asp.net...

    Cmq ti ringrazio per l'aiuto... adesso provo il codice che hai corretto e ti faccio sapere. Grazie ancora...
    "Quando si mangia il formaggio svizzero, che succede ai buchi?" (B. Brecht)

    Visitate il mio sito:
    www.clamorosalcibali.it

  5. #5
    Non va...

    Codice PHP:
    Compiler Error MessageBC30451Name 'i' is not declared.

    Source Error:

    Line 27:     upload_ok False
    Line 28
    :     If LCase(Request.ServerVariables("REQUEST_METHOD")) = "post" Then
    Line 29
    :         For 0 To Request.Files.Count 1
    Line 30
    :             fileFld Request.Files(i)  ' get the first file uploaded from the form (note:- you can use this to itterate through more than one image)
    Line 31:             If fileFld.ContentLength > upload_max_size * 1024 Then 

    EDIT:

    Come non detto... ho aggiunto il 'Dim i' e ora funziona!!!
    "Quando si mangia il formaggio svizzero, che succede ai buchi?" (B. Brecht)

    Visitate il mio sito:
    www.clamorosalcibali.it

  6. #6
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    Non è che il codice sia brutto, è che codice asp.net stile asp non credevo neanche funzionasse
    Pietro

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.