Subir archivos con ASP
Utilice la función «Imprimir» al final de la página para crear un PDF.
Información válida para: planes de Hosting Windows
Puedes subir archivos en el navegador usando ASP. Los siguientes scripts de ejemplo te muestran cómo hacerlo:
ASP
MaxLength = x especifica el tamaño máximo del archivo en bytes
<head>
<title>file upload via ASP</title>
</head>
<body bgcolor="white">
<%=Font%>
<%
' --- ASP FileUpload Module GetFILE (C) 1999 by Stefan Falz
' --- Activate error handling
' --- On Error Resume Next
' --- Assign the error source that is to be output if an error occurs.
Err.Source = "GetFILE HTTP Upload"
' --- Constant for error heading
Const ErrHeader = "<b>Error</b><br><br>"
' --- Declare array
Dim ErrArray(4)
ErrArray(0) = "10900 - Your sent file is too large."
ErrArray(1) = "10901 - Unknown error.<br>"
ErrArray(2) = "10902 - No file has been sent or the transfer is faulty.<br>"
ErrArray(3) = "10903 - No text file has been sent.<br>"
' -- Call the GetFILE subroutine
Call GetFILE()
Private Sub GetFile()
' --- The HTTP stream is read in this subroutine.
' --- Declare the variables
Dim FileText
FileText = Request.BinaryRead(Request.TotalBytes)
Dim FileTextByte
FileTextByte = """
Dim FileTextNew
FileTextNew = """
Dim FilePosFirst
FilePosFirst = 0
Dim FilePosLast
FilePosLast = 0
Dim FileType
<font color="#ffff00">-==- proudly presents
Dim strRevText
<font color="#ffff00">-==- proudly presents
Dim strFileName
<font color="#ffff00">-==- proudly presents
Dim strFileNameOnly
<font color="#ffff00">-==- proudly presents
Dim posFileName
posFileName = 0
Dim rghFile
<font color="#ffff00">-==- proudly presents
Dim lenFileTextByte
lenFileTextByte = 0
' --- Specify the max. file size + approx. 500 bytes for file information
Dim maxLength
maxLength = 25500
' --- Query the size of the sent stream
If Request.TotalBytes > maxLength Then
Call Error_Handler(10900)
Exit Sub
End if
' --- Search for HexCode 0D 0A 0D 0A (beginning of file within HTTP stream)
FilePosFirst = InStrB(FileText, (ChrB(13) & ChrB(10) & ChrB(13) & ChrB(10)))
' --- Search for HexCode 0D 0A 2D 2D (end of file within HTTP stream)
FilePosLast = InStrB(FileText, (ChrB(13) & ChrB(10) & ChrB(45) & ChrB(45))
' --- Search for HexCode 2D 0A 0D within the reversed HTTP stream, because under
' --- There may be several end-of-file indicators.
strRevText = StrReverse(FileText)
FilePosLast = InStrB(strRevText, (ChrB(45) & ChrB(10)) & ChrB(13))
FilePosLast = LenB(FileText) - FilePosLast
' --- Abort at file size 0 Byte
If FilePosFirst = 0 Or FilePosLast = 0 Or FilePosLast - FilePosFirst < 5 Then
Call Error_Handler(10902)
Exit Sub
End If
' --- Cancel if "Content disposition" is not specified
If InStrB(FileText, ChrB(67) & ChrB(111) & ChrB(110) & ChrB(116) & ChrB(101) & ChrB(110) & ChrB(116) & ChrB(45) & ChrB(84) & ChrB(121) & ChrB(112) & ChrB(101) & ChrB(58) & ChrB(32) & ChrB(116) & ChrB(101) & ChrB(120) & ChrB(116) & ChrB(47)) = 0 Then
Call Error_Handler(10903)
Exit Sub
End if
' --- Cancel if an error has occurred that is not one of the above errors
If Err.Number <> 0 Then
Call Error_Handler(10901)
Exit Sub
End if
' --- Determination of the file name incl. path within the HTTP stream
posFileName = InStrB(FileText, ChrB(102) & ChrB(105) & ChrB(108) & ChrB(101) & ChrB(110) & ChrB(97) & ChrB(109) & ChrB(101) & ChrB(61) & ChrB(34)) + 9
rghFileText = RightB(FileText, LenB(FileText) - posFileName)
strFileName = LeftB(rghFileText, InStrB(rghFileText, ChrB(34)) - 1)
Response.Write "<strong>File name incl. path: </strong>"
Response.BinaryWrite strFileName
Response.Write "<br>"
' --- Determination of the file name excl. path within the HTTP stream
posLastSlash = InStrB(StrReverse(strFileName), ChrB(92))
strFileNameOnly = MidB(strFileName, LenB(strFileName) - posLastSlash, posLastSlash + 1)
Response.Write "<strong>File name excl. path: </strong>"
Response.BinaryWrite strFileNameOnly
Response.Write "<br>"
' --- Cancel if an error occurred
If Err <> 0 Then
Call Error_Handler(10901)
Exit Sub
End if
' --- Writing the file contents in ByteArray
FileTextByte = MidB(FileText, (FilePosFirst + 4), (FilePosLast - (FilePosFirst + 4)))
' --- Determining the file size by reading the length of the determined file content
lenFileTextByte = LenB(FileTextByte)
' --- Conversion of the file content (binary stream) to Ascii characters
For i = 1 To lenFileTextByte
FileTextNew = FileTextNew & Chr(AscB(MidB(FileTextByte, i, 1)))
Next
' --- Write the determined file contents to a file.
Set objFileSys = Server.CreateObject("Scripting.FileSystemObject")
Set File = objFileSys.CreateTextFile(Server.MapPath("./") & "\" & Session.SessionID & ".tmp", True, False)
File.WriteLine CStr(FileTextNew)
File.Close
Set File = Nothing
Set objFileSys = Nothing
' --- Output the file size in bytes
Response.Write "<strong>Dateigröße: </strong>" & lenFileTextByte & " Byte.<br><br>"
' --- Convert file content because HTML pages are not displayed correctly
FileTextNew = Replace(FileTextNew, "<", "<")
FileTextNew = Replace(FileTextNew, ">", ">")
FileTextNew = Replace(FileTextNew, VbCrLf, "<br>" & VbCrLf)
FileTextNew = Replace(FileTextNew, Chr(9), " ")
' --- Output of the file content
Response.Write FileTextNew
End Sub
Private Sub Error_Handler(intErrNumber)
' --- Determination of the transferred error code and output on the screen
Select Case intErrNumber
Case 10900: Response.Write ErrHeader & ErrArray(0)
Case 10901: Response.Write ErrHeader & ErrArray(1)
Case 10902: Response.Write ErrHeader & ErrArray(2)
Case 10903: Response.Write ErrHeader & ErrArray(3)
Case Else: Response.Write ErrHeader & Err.Description
End Select
Exit Sub
End Sub
%>
</body>
</html>
En este ejemplo, un formulario HTML simple sirve como interfaz de usuario.
<html>
<head>
<title>file upload via ASP</title>
</head>
<body>
<form method="POST" action="upload.asp" enctype="multipart/form-data" target="_new">
<input type="file" name="File" size="50"><br>
<input type="submit" value="START UPLOAD" name="Submit">
</form>
</body>
</html>
ASP.NET
Con ASP.NET el script de carga se puede realizar de la siguiente manera:
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Specify the path on the server to
' save the uploaded file to.
Dim savePath As String = "e:\Clientes\Sitiosweb\11\d12345678\www\UploadTest"
' Before attempting to perform operations
' on the file, verify that the FileUpload
' control contains a file.
If (FileUpload1.HasFile) Then
' Get the name of the file to upload.
Dim fileName As String = FileUpload1.FileName
' Append the name of the file to upload to the path.
savePath += fileName
' Call the SaveAs method to save the
' uploaded file to the specified path.
' This example does not perform all
' the necessary error checking.
' If a file with the same name
' already exists in the specified path,
' the uploaded file overwrites it.
FileUpload1.SaveAs(savePath)
' Notify the user of the name the file
' was saved under.
UploadStatusLabel.Text = "Your file was saved as " & fileName
Else
' Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload."
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>FileUpload Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h4>File to upload:</h4>
<asp:FileUpload id="FileUpload1"
runat="server">
</asp:FileUpload>
<br /><br />
<asp:Button id="UploadButton"
Text="Upload file"
OnClick="UploadButton_Click"
runat="server">
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server">
</asp:Label>
</div>
</form>
</body>
</html>
Nota
En la siguiente línea todavía tienes que introducir el ruta correcta a tu presencia:
Dim savePath As String = "e:\Clientes\Sitiosweb\11\d12345678\www\UploadTest"