Friday, October 16, 2009

Is It Possible To Turn Into A Wolf

Upload Files ASP components

Want to upload files to the server via a form, with one or more fields of type FILE, but ASPUpload can not use, do not want to pay or can not register components. Here is a complete solution for file upload, written 100% in ASP and VBScript.




Introduction
Upload files to the server using a form is an option that provides powerful HTML in

as allowing us to create complete web applications: we can make our own system

Home FTP, web, we can let our partners upload their images, etc.
all through a web page.
Unfortunately, ASP does not provide an automatic mechanism to process this type of shipment. Therefore,
the need to extend the ASP, along with commercial products and SA-FileUp ASPUpload or
writing our own program.
The problem of using components, whether owned or purchased (or pirated ... Who said that?) Is
in many hosting plans we can not register these components. In these cases, the only solution is to program
by pure ASP code, and that's what I done. Requirements




xelUpload

is the name of a VBScript class that has properties and methods to handle sending files to the server. His only requirement is to have version 5 or above script engine. To see which version you have available, you can write: Response.Write

ScriptEngineMajorVersion
If you see a 5, everything is correct. If not, you can download the latest version from the Microsoft site on
script, or install Internet Explorer 5 or higher , which includes among its files. As if you did not know, so the file arrives correctly server, we need to add another attribute to the brand

FORM, which specifies the encoding of the form to send it, and allows us to upload binary data well: \u0026lt;form action = "pagina.asp" method = "post "
enctype =" multipart / form-data "> How to use

How does xelUpload

? The first thing we do is include the file by: \u0026lt;!--# include file = "xelupload.asp" ->
if the same directory or \u0026lt;!--# include
virtual = "/ includes / xelupload.asp" ->
if another virtual path, in this case, / includes /
. Then we create an instance of the object, and call the Upload
to start the process on the page that receives the form.

\u0026lt;% Dim objUploadobjUpload.Upload ()%>
  xelUpload  
object, once called his method Upload, store the files sent with the form in the collection files, and fields normal text library eltosForm . The first contains objects of type File , another class that also included, and second elements can be accessed with objUpload.Form ("field")
, which is quite familiar. ;-) for the number of files uploaded correctly, we can use: Response.Write
objUpload.Ficheros.Count & "uploads."
To access the properties and methods of each
file uploaded, we can iterate through the collection, or direct access to one, with the name of the INPUT field have the form: Dim
objUpload, objFicheroobjUpload.Upload () 'If we \u0026lt;input type="file" September name="imagen"> objFichero = objUpload.Ficheros ("image")' Loop all: for each objFichero in objUpload.Ficheros.Items ... next
  NOTE: The ZIP file with the code, documentation and an example is at the end of the article. Properties and methods  

to see in detail what we can do with the files once uploaded, see what offers xelUpload

class and class file , respectively
xelUpload Properties Form (drivername) Request.Form (drivername) Methods Upload . It is essential Collections Size TipoContenido return "image / gif" Data Returns Methods Save GuardarBD



Returns form field named "drivername." Completely equivalent
.


Make the process of receiving the form and stores the results

call this method before accessing the other object properties or collections
xelUpload.




files contains a collection of objects
file, one for each file sent from the form correctly.

File Properties

Name
Returns the original name of the file without
the path in the user's disk. For example, mickeymouse.gif .

Returns the size in bytes of the file.


Returns the content type of the file into a string, if recognized by the server, or "application / octet-stream." For example, for the previous image,
.

pure binary data file.


route
Save the file to disk on the server, with the original name in the path passed as an argument. Must be a physical path, not virtual, so you probably use Server.MapPath
well:
objFichero.Guardar (Server.MapPath ("\\ upload"))

SaveAs name, path
Save the file to disk, with the specified name in the path specified. The name must be a valid file name and path, a physical path.

objFld
Saves the contents of the file in order
ADO Field passed as argument. Below I explain how to do this.
see, the interface is quite flexible. As always, this looks better with an example, so let it.
Suppose we send a form like this:


action="pagina.asp" \u0026lt;form method="post" enctype="multipart/form-data"> Name: \u0026lt;input type = "text" name = "user" size = "20"> \u0026lt;br> Age: \u0026lt;input type="text" name="edad" size="5"> \u0026lt;br> \u0026lt;input type = "file" name = "image" > \u0026lt;input type="submit" \u0026lt;br> value="Enviar"> \u0026lt;/ form>
  On the page that greets you and you specified in the ACTION attribute,  pagina.asp 
, we use Our super-versatile xelUpload (notice that I'm happy, eh?) for the type of file and send us your size. If it is a GIF image and does not exceed 15k (15 * 1024 bytes), store it in two directories different (okay, it is not so normal, but I have to illustrate the methods) and a database, in a hypothetical table that keeps users (can you imagine?) the name, age, and a GIF ( User supposedly, although GIF and less than 15k ... ahem). not going to save only the file name: we will store the entire image in a database Access.
This is the code of your page that receives the form


\u0026lt;% 'pagina.asp %>\u0026lt;!--# include file = "xelupload.asp "-->\u0026lt;% Dim oConn, rs, SQLDim objUpload, objFich, strName, strEdadDim maxtam, strNombreFichero'nuestro máximomaxtam size = 15 * 1024set xelUploadobjUpload.Upload objUpload = new () 'Retrieve Fields = objUpload.Form edadstrNombre user ("user") = objUpload.Form Streda ("age")' if not upload any file , we avisamosif objUpload.Ficheros.Count = 0 thenResponse.Write ("You have not uploaded any files!") else'accedemos the file by name in the formularioset objFich = objUpload.Ficheros ("image") if objFich.Tamano \u0026lt; ; = maxtam and objFich.TipoContenido = "image / gif" then'Es the right file, we will save all parties. :-) "First, in our current directory with the name Server.MapPath tieneobjFich.Guardar (".")' Now in the upload directory with today's date as nombrestrNombreFichero = Day (Date) & Month (Date) & Year (Date) objFich.GuardarComo strNombreFichero, Server.MapPath ("upload") 'Now we connect to a database datosset oConn = Server.CreateObject ("ADODB.Connection") set rs = Server.CreateObject ("ADODB.Recordset") 'We connect to ODBC DSN, base'de assuming the data is in the same directoriooConn.Open "Driver = {Microsoft Access Driver (*. mdb)}; "_ &" DBQ = "& Server.MapPath (" usuarios.mdb ") SQL =" SELECT * FROM Users "'Open the Recordset: 0 is the cursor adForwardOnly 'and 2 is the bolt adLockPessimisticrs.Open SQL oConn, 0, 2'Añadimos a new registrors.AddNewrs.Fields ("Name") = strNombrers.Fields ("Age") = strEdadrs.Fields ("FileName") = objFich.Nombre 'We call the corresponding field by passing GuardarBD! ! objFich.GuardarBD rs.Fields ("Picture") 'Save the new registrors.Update' and all that come clean padresrs.Closeset nothingoConn.Closeset rs = oConn = nothing 'All right! Response.Write "has been added the new user \u0026lt;b> "& strName &" \u0026lt;/ b>, "_ &" to file "& objFich.Nombre &" Size "& objFich.Tamano &" '. " clean our xelUpload and ficheroset oFich = nothingset objUpload = válidoResponse.Write nothingelse'No was a file (a file is not acceptable. ") Response.Write (" You must be a GIF file less than 15k. ") end if IfEnd %> This code
  you see all the possibilities xelUpload  
. Table users that use this structure:
user_ID Text Age Text Image OLE Object
Field Name Type
data



AutoNumber Name





FileName Text



As you see,

OLE object is the type that accepts binary Access. In SQL Server the equivalent is image, which is binary and can support more than 8,000 bytes, or binary if we keep less than 8,000 bytes. comments, notes, etc. There is a major constraint
Request object, making it essential to include the handling of the standard fields of a form within the program that handles files Uploaded: after using Request.BinaryRead

can not use Request.Form and vice versa. So you have to use Form property of your object xelUpload to read the usual fields of a form. Source:
AspFacil
File: Xelupload

xelupload.zip

0 comments:

Post a Comment