ASP upload pictures Without components of the basic method

Source: Internet
Author: User
Tags file upload html form
Upload | upload pictures | No components Below is an example of an ASP upload image without components:

<body>
<form enctype= "Multipart/form-data" action= "upload.asp" method= "POST" >
Please select the file to upload: <br>
<input name= "Picture" Type=file accept= "image/*" ><br>
<input type=submit value= "Upload" ></form>
</body>

When an HTML file is displayed under Netscape Navigator (version 3.0) or Internet Explorer (version 4.0), a seemingly plain text box appears next to the Browse key. Users can type the file name directly into a text box, or use the Browse key to select a file from the file dialog box. When the form is submitted, the user's selected files are submitted concurrently.
The enctype characteristics of the <form> label specify the type of encoding to request the form information. Form information is generally based on the URL coding system. However, when uploading files, you must use the Enctype= "Multipart/form-data" feature.
The actual file upload key is created with the following tag:
<input name= "Picture" Type=file accept= "image/*" >
Because the type has a characteristic value of file, the browser creates a file upload key. The Accept feature restricts the specific files that appear in the file dialog box, in which case the file type is limited to the diagram file. A manifest of any MIME type can be specified as a accept characteristic value.
File upload keys are very useful, unfortunately many browsers only partially support upload keys. For example, Netscape Navigator (version 3.0 and 4.0) ignores the Accept feature, IE 3.0 is even worse, it simply does not explain the upload key, but displays the normal text box. However, the IE 4.0 and 5.0 versions can identify file upload keys.
Creating an HTML form with a file upload key is just the first step in uploading a file, and submitting a file with an upload key cannot get the contents of the file using the normal method of the Request object. The request object in ASP has a special method specifically used for this purpose. When the form is mailed, the BinaryRead () method of the Request object is used to read the submitted raw bytes. Note: Once the BinaryRead () method is used in the ASP, it is not possible to get the form data using the normal Request.Form () method (and vice versa).
Assuming the user submits a picture using the previously mentioned form, you can use the following script to obtain and display the original bytes of the submission.

<%
Formsize=request.totalbytes
Formdata=request.binaryread (Formsize)
Response.BinaryWrite FormData
%>

The script above uses the TotalBytes feature of the request object to get the number of bytes sent to the HTTP site. All the form data is then assigned to a variable named Formdata. Finally, the uploaded data content is output using the BinaryRead () method of the Response object.
But the script output in this way is not fully readable, and it contains all the raw data for the uploaded file and important information about the contents of the file.
The data can be saved in the database, in the file, or simply displayed, and can even quickly change the original image data to create a custom image. The following script removes the file headers and displays the files that are uploaded as images in the browser.

<%
Formsize=request.totalbytes
Formdata=request.binaryread (Formsize)
BNCRLF=CHRB (+) & ChrB (10)
Divider=leftb (FORMDATA,INSTRB (FORMDATA,BNCRLF)-1)
DATASTART=INSTRB (Formdata,bncrlf & Bncrlf) +4
DATAEND=INSTRB (Datastart+1,formdata,divider)-datastart
Response.contenttype= "Image/gif"
Response.BinaryWrite MIDB (Formdata,datastart,dataend)
%>

The above script uses the INSTRB () function to pick up the image data from the form data to find the starting position. It assumes that the image data starts with two carriage returns and newline characters and ends with the same delimiter. The contents of the response object's contentype are then sent to the browser with GIF images. Finally, the BinaryWrite () method is used to output the data. The picture is displayed on the browser.
The script above assumes that the submitted table contains only one field containing the image data. However, HTML forms that contain file upload elements can also contain other standard elements, such as text boxes, text areas, and so on.
Although complex forms can be handled with standard ASP scripts, it is best to use Third-party ASP components to work with forms that contain file upload elements in the same way that you do with standard HTML forms.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.