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.