Pagina 1 di 3 1 2 3 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 22

Discussione: upload. non funge!

  1. #1
    Utente bannato
    Registrato dal
    Oct 2003
    Messaggi
    372

    upload. non funge!

    salve a tutti. sto tentando di sviluppare la mia prima applicazione web con ASP.NET. ho appena fiito di leggere il manuale, ed ho pensato di creare un piccolissimo sistema di upload di un file usando il solo file aspx.

    il mio codice è questo:

    codice:
    <%@  import namespace="System"%>
    <%@  import namespace="System.web"%>
    <%@  import namespace="System.web.UI"%>
    <%@  import namespace="System.web.UI.WebControls"%>
    <%@  import namespace="System.web.UI.HtmlControls"%>
    <%@  import namespace="System.IO"%>
    
    
    <script language="VB" runat="server">
    
    
    Public Class uploadFile
    	inherits page
    	protected withEvents CmdUpload as button
    	protected lblInfo as Label
    	protected fileInput as htmlInputFile
    	
    	Private sub page_load(sender as object, e as eventArgs) handles MyBase.load
    		fileInput.accept = "image/*"
    	end sub
    	
    	Private Sub CmdUpload_click(sender as object, e as eventArgs) handles CmdUpload.click
    		if fileInput.postedFIle is nothing then
    			lblInfo.text="Specificare file"
    		else
    			try
    				dim serverfilename as string
    				serverFilename=path.getFileName (fileInput.postedFile.FIlename)
    				fileinput.postedFile.saveAs("F:\root\asp.net\"&serverfilename)
    				lblInfo.text="File " & serverfilename & " caricato correttamente"
    				catch err as exception
    					lblInfo.text=err.message
    			end try
    		end if
    	end sub
    end class
    </script>
    
    <body>
    <form id="form" runat="server" enctype="multipart/form-data">
    <input type="file" id="fileInput" runat="server" />
    <input type="submit" id="CmdUpload" value="up" runat="server" />
    <asp:label id="lblInfo" runat="server" />
    </form>
    </body>

    stranamente accade che non da nessun errore e non carica nessun file.
    sapreste illuminarmi? di sicuro ho commesso un errore, ma più che sapere la semplice soluzione mi piacerebbe che qualcuno mi spiegasse il concetto dell'errore.

    grazie a tutti.

  2. #2
    Utente di HTML.it L'avatar di kluster
    Registrato dal
    Jul 2003
    Messaggi
    1,288
    Dunque, premetto che anche se in asp classic sviluppo in vbscript in asp.net utilizzo C# quindi vado un po a tentoni.

    Nell'utilizzo della tua classe vedo che tu istanziavi un oggetto di tipo
    protected withEvents CmdUpload as button

    mentre nell'aspx utilizzavi un

    <input type="submit" id="CmdUpload" value="up" runat="server" />
    che invece è di tipo htmlInputbutton, allo stesso modo l'evento scatenato dall'oggetto htmlInput è ServerClick non Click


    quindi questo è il file upload.vb

    codice:
    Imports System.web.UI
    Imports System.web.UI.WebControls
    Imports System.web.UI.HtmlControls
    Imports System.IO
    Public Class uploadFile
    	inherits page
    	protected withEvents CmdUpload as htmlInputbutton 
    	protected lblInfo as Label
    	protected fileInput as htmlInputFile
    
    	Private sub page_load(sender as object, e as eventArgs)
    		fileInput.accept = "image/*"
    	end sub
    	Private Sub CmdUpload_ServerClick (sender as object, e as eventArgs) handles CmdUpload.ServerClick 
    		if fileInput.value = "" then 'con il tuo controllo non era mai nothing cosi controlla il valore inserito
    			lblInfo.text="Specificare file"
    		else
    			try
    				dim serverfilename as string
    				serverFilename=path.getFileName (fileInput.postedFile.FIlename)
    				fileinput.postedFile.saveAs("F:\root\asp.net\"&serverfilename)
    				lblInfo.text="File " & serverfilename & " caricato correttamente"
    				catch err as exception
    					lblInfo.text=err.message
    			end try
    		end if
    	end sub
    end Class
    cosi lo includi nella pagina aspx

    codice:
    <%@ Page Language="VB" Src="upload.vb"  inherits="uploadFile" %>
    <html>
    <body>
    <form id="form" runat="server" enctype="multipart/form-data">
    <input type="file" id="fileInput" runat="server" />
    <input type="submit" id="CmdUpload" value="up" runat="server"/>
    <asp:label id="lblInfo" runat="server" />
    </form>
    </body>
    </html>

  3. #3
    Utente bannato
    Registrato dal
    Oct 2003
    Messaggi
    372
    mmm.... bene, ti ringrazio. ma stranamente ancora continua a non caricare il file senza restituire errori.

    carico un file jpg e il processo mi restituisce il medesimo form senza nessun tipo di messaggio. controllo poi la cartella e il file non è stato caricato.

    chiaramente la direcory è valida. fra l'altro è la stessa dove sta la pagina in questione.

  4. #4
    Utente di HTML.it L'avatar di kluster
    Registrato dal
    Jul 2003
    Messaggi
    1,288
    l'ho testato e funge , nota che io ho spezzato in upload.vb e upload.aspx

  5. #5
    Utente bannato
    Registrato dal
    Oct 2003
    Messaggi
    372

    anomalie

    ok, ora ho spezzato anche in 2 file. (.vb e .aspx)
    si funge ma con qualche anomalia

    1) il controllo sul tipo di file non funziona. dovrebbe accettrare solo i tipi Immagine, ma.... mi fa passare anche gli altri tipi.

    2) il controllo su
    if fileInput.value = ""
    non funziona garnchè, nel senso, se metto il cpontrollo che mi hai indicato non mi fa caricare sessun file, mi dice sempre "Specificare file", se uso "is nothing" invece fa il contrario, cioè come dicevi tu, non riconosce il valore vuoto. e (sempre con "if is nothing") se lascio la stringa vuota m dice che il path non è corretto.

    ho provato anche con
    codice:
    ...
    dim str as string 
    str=fileInput.value
    if str.length<6 then
    ....
    ma mi si comporta come nel caso di if fileInput.value = ""


    come devo fare? e soprattuto perchè?

  6. #6
    Utente di HTML.it L'avatar di kluster
    Registrato dal
    Jul 2003
    Messaggi
    1,288
    il controllo su If fileInput.value = "" then a me funziona correttamente: se non carico nulla e premo up mi dice "specificare il file" mentre se carico un qualsiasi file mi carica correttamente il file, non so come mai a te non funziona.

    il filetype non funziona hai ragione, poi faro 2 prove cmq per farlo andare basta un
    codice:
    Private Sub CmdUpload_ServerClick(sender as object, e as eventArgs) handles CmdUpload.ServerClick
            if fileInput.value = "" then
    			lblInfo.text="Specificare file"
    		else
    			if fileInput.PostedFile.ContentType.StartsWith("image")  then 
    				try
    					dim serverfilename as string
    					serverFilename=path.getFileName (fileInput.postedFile.FIlename)
    					fileinput.postedFile.saveAs("G:\aspx\"&serverfilename)
    					lblInfo.text="File " & serverfilename & " caricato correttamente"
    					catch err as exception
    						lblInfo.text=err.message
    				end try
    			else
    				lblInfo.text			= "E' possibile caricare solo immagini"
    			end if 
    		end if 	
    end sub

  7. #7
    Utente bannato
    Registrato dal
    Oct 2003
    Messaggi
    372
    ok, ora il tipo immagine funziona. ma il controllo sul file inserito o non inserito ancora no. se provo a stampare fileInput.value in lblInfo non mi stampa nulla anche se vado a inserire un file immagine.

  8. #8
    Utente di HTML.it L'avatar di kluster
    Registrato dal
    Jul 2003
    Messaggi
    1,288
    Non so che dirti, forse hai qualche cosa in + nel codice rispetto a quanto ho io, perchè a me funziona

  9. #9
    Utente bannato
    Registrato dal
    Oct 2003
    Messaggi
    372
    si, in effetti. allora, ecco il codice del file upload.vb
    codice:
    Imports System
    Imports System.web
    Imports System.web.UI
    Imports System.web.UI.WebControls
    Imports System.web.UI.HtmlControls
    Imports System.IO
    Public Class uploadFile
    		
    		inherits page
    		protected withEvents CmdUpload as htmlInputbutton 
    		protected lblInfo as Label
    		protected fileInput as htmlInputFile
    		protected path_img as htmlInputText
    		protected gra as htmlImage
    		
    		private sub Page_load(sender as object, e as eventArgs) handles MyBase.load
    				gra.visible = false
    		end sub
    		
    		
    		Private Sub CmdUpload_ServerClick(sender as object, e as eventArgs) handles CmdUpload.ServerClick
    				'if fileInput.PostedFile is nothing then
    				if fileInput.value="" then
    						lblInfo.text="Specificare file"
    				else
    						if fileInput.PostedFile.ContentType.StartsWith("image")  then 
    								try		
    										dim file_path as string
    										dim pre_file as string
    										dim serverfilename as string
    										serverFilename=path.getFileName (fileInput.postedFile.FIlename)
    										dim dirc as new directoryInfo(server.mappath("/" & path_img.value))
    										if dirc.exists then
    											pre_file="/" & path_img.value &  "/" & serverfilename
    										else
    											pre_file = path_img.value &  "/" & serverfilename
    										end if
    										file_path=server.mappath(pre_file)
    										fileinput.postedFile.saveAs(file_path)
    										lblInfo.text="File " & serverfilename & " caricato correttamente in " & file_path
    										gra.src = pre_file
    										gra.alt="Imamgine"
    										gra.visible=true
    										catch err as exception
    										lblInfo.text=err.message
    								end try
    						else
    								lblInfo.text = "E' possibile caricare solo immagini"
    						end if 
    				end if 	
    		end sub
    		
    end Class
    ecco invece upload.aspx
    codice:
    <script>
    function check()
    {
    	valore=document.form.fileInput;
    	if (valore.value.length<6) {alert("specificare file"); valore.focus(); return false }
    }
    </script>
    <body>
    <form id="form" onSubmit="return check()" runat="server" enctype="multipart/form-data">
    carica nella directory <input type="text" name="path_img" id="path_img" runat="server" />
    
    <input type="file" id="fileInput" name="fileInput" runat="server" />
    <input type="submit"  id="CmdUpload" name="CmdUpload" value="up" runat="server" />
    <asp:label id="lblInfo" runat="server" />
    </form>
    
    <img id="gra" runat="server" />
    </body>
    come puoi notare per ho inserito un controllo client-side. per testare (magari ti vada di farlo) toglilo

  10. #10
    Utente di HTML.it L'avatar di kluster
    Registrato dal
    Jul 2003
    Messaggi
    1,288
    Nulla a me funziona:
    file upload.vb:
    codice:
    Imports System
    Imports System.web
    Imports System.web.UI
    Imports System.web.UI.WebControls
    Imports System.web.UI.HtmlControls
    Imports System.IO
    Public Class uploadFile: Inherits page
    	protected withEvents CmdUpload as htmlInputbutton 
    	protected lblInfo as Label
    	protected fileInput as htmlInputFile
    	protected path_img as htmlInputText
    	protected gra as htmlImage
    	
    	private sub Page_load(sender as object, e as eventArgs) handles MyBase.load
    		gra.visible = false
    	end sub
    	
    	Private Sub CmdUpload_ServerClick(sender as object, e as eventArgs) handles CmdUpload.ServerClick
    		if fileInput.value				= "" then
    			lblInfo.text				= "Specificare file"
    		else
    			if fileInput.PostedFile.ContentType.StartsWith("image")  then 
    				try		
    					dim file_path as string
    					dim pre_file as string
    					dim serverfilename as string
    					serverFilename		= path.getFileName (fileInput.postedFile.Filename)
    					dim dirc as new directoryInfo(server.mappath("/" & path_img.value))
    					if dirc.exists then
    						pre_file		= "/" & path_img.value &  "/" & serverfilename
    					else
    						pre_file 		= path_img.value &  "/" & serverfilename
    					end if
    					file_path			= Server.mappath(pre_file)
    					fileinput.postedFile.saveAs(file_path)
    					lblInfo.text		= "File " & serverfilename & " caricato correttamente in " & file_path
    					gra.src 			= pre_file
    					gra.alt				= "Imamgine"
    					gra.visible			= true
    				catch err as exception
    					lblInfo.text		= err.message
    				end try
    			else
    				lblInfo.text			= "E' possibile caricare solo immagini"
    			end if 
    		end if 	
    	end sub
    end Class
    il file aspx:
    codice:
    <%@ Page Language="VB" Src="upload.vb"  inherits="uploadFile" %>
    <html>
    <body>
    <form enctype="multipart/form-data" runat="server">
    	carica nella directory
    
    	<input type="text"   id="path_img"  runat="server" />
    
    	<input type="file"   id="fileInput" runat="server" />
    	<input type="submit" id="CmdUpload" value="up" runat="server" />
    	<asp:label id="lblInfo" runat="server" />
    	
    <img id="gra" runat="server" />
    </form>
    </body>
    </html>
    ed il controllo funziona perfettamente, nel caso ti mando un link in private dove ho messo l'esempio che ho appena postato

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.