Several Methods for uploading files using Asp.net Input

Source: Internet
Author: User

When the client uploads a file to the server, sometimes we want to click a button or a tag, the Select File Dialog Box will pop up automatically,

< Input type = " File " ID = " File " Style = " Display: None " > < BR />
< Input type = " Button " Value = " Click " Onclick = " OK () " > < BR />

Javascript OK method:

Function OK ()
{
VaR s
= Document. getelementbyid ( " File " );
S. style. Display
=   " Block " ;
S. Click
=   True ;
VaR Str
= S. value;
Alert (STR );
}

The preceding STR is the path of the file to be uploaded by the client,

However, in Asp.net

<Input type='File' />


The size of the file to be uploaded by the control is limited. By default, the file size is about 4 MB. If a large file is uploaded, an error cannot be displayed on the page.

Modify the parameters in the web. config file to set the size of the file to be uploaded by the control. The configuration in Web. config is as follows: add

<Httpruntime maxrequestlength="409600"/>

You can set

<Input type='File' />

Control the size of the file to be uploaded. maxrequestlength is measured in bytes.

First upload method: (HTML-only upload or upload of different files)

Page file:
< Form ID = " Form1 " Method = " Post " Enctype = " Multipart/form-Data " Action = " Details. aspx " >
< Input ID = " Testid " Type = " File " Name = " Testname " />
< Input ID = " Testsubmit " Type = " Submit " Value = " Upload "   />
</ Form >

Background Code :
Protected   Void Page_load ( Object Sender, eventargs E)
{
/* Files will be upload when PostBack */
If ( ! Ispostback)
{
If ( This . Request. Files. Count >   0 )
{
Httppostedfile F
=   This . Request. Files [ 0 ];
String Fname = F. filename;
/* Startindex */
Int Index = Fname. lastindexof ( " \\ " ) +   1 ;
/* Length */
Int Len = Fname. Length - Index;
Fname
= Fname. substring (index, Len );
/* Save to server */
F. saveas (
This . Server. mappath ( " ~ /Files/ "   + Fname ));
Response. Write (
" <SCRIPT type = 'text/JavaScript '> alert ('success! '); </SCRIPT> " );
}
}
}

Method 2: (runat = "server ")

Page file:
< Input type = " File " Runat = " Server " ID = " Testfile " />
< Input type = " Submit " ID = " Testsubmit " Value = " Upload "   />
< BR />

Background code:
If ( ! Ispostback)
{
/* Input Upload File */
If (Request. Files. Count >   0 )
{
Httppostedfile F
= Request. Files [ 0 ];
String Fname = F. filename;
Int Index = Fname. lastindexof ( " \\ " );
Int Len = Fname. Length - Index -   1 ;
Fname
= Fname. substring (Index +   1 , Len );
F. saveas (
This . Server. mappath ( " ~ /Files/ "   + Fname ));
Response. Write (
" <SCRIPT type = 'text/JavaScript '> alert ('success! '); </SCRIPT> " );
}
}

the and methods are different.

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.