Asynchronous File Upload ASP. NET

Source: Internet
Author: User
Asynchronous File Upload ASP. NET (for example, uploading images above)
HTML code:
<Head> // The two JS
<SCRIPT src = "jquery-1.4.2.min.js" type = "text/JavaScript"> </SCRIPT>
<SCRIPT src = "ajaxfileupload. js" type = "text/JavaScript"> </SCRIPT>
</Head>

<Input id = "FUP" name = "FUP" type = "file"/>
<Input id = "btnup" type = "button" value = "Upload" onclick = "ajaxfileuploads ();"/>

JS Code:
Function ajaxfileuploads ()
{
$. Ajaxfileupload (
{
URL: 'ajax _ fileupload. aspx,
Secureuri: false,
FileElementId: 'fup ',
DataType: 'json ',
Success: function (data, status)
{
If (data. err = "")
{
Document. getElementById ("img"). style. display = "block ";
Document. getElementById ("img"). src = data. msg;
}
Else
{
$ ('# Sperr'). text (data. err );
}
},
Error: function (data, status, e)
{
$ ('# Sperr'). Text ("Upload Failed! ");
}
});
}

Ajax_fileupload.aspx page:
Protected void page_load (Object sender, eventargs E)
{
Httpfilecollection files = system. Web. httpcontext. Current. Request. files;
Httppostedfile file = files ["FUP"];
If (file. contentlength <= convert. toint32 (sizefunction () * 1024*1024) // sizefunction () to get the file size limit, you can write it in the configuration file to limit the upload size.
{
If (isvalidfiletype (file. filename) // verify the file type
{
String picPaht = Server. MapPath (FileFunciton (); // path for saving the FileFunction () file. Set the name as required. I have the simplest date here.
Random rd = new Random ();
String name = DateTime. Now. ToString ("yyyyMMddhhmmssfff") + rd. Next (1000) + GetExtension (file. FileName );
String strNewPath = GetSaveFilePath (picPaht) + name;
File. SaveAs (strNewPath );
Response. Write ("{msg: '" + dal. ImgUrl () + name + "', err :''}");
}
Else
{
Response. Write ("{msg:'', err: 'type error! '}");
}
}
Else
{
Response. Write ("{msg:'', err: 'size error! '}");
}
}

/// <Summary>
/// Verify the file type
/// </Summary>
/// <Param name = "FileName"> </param>
/// <Returns> </returns>
Private bool IsValidFileType (string FileName)
{
String [] _ AcceptedFileTypes = TypeFunction (). Split ('|'); // type function () to obtain the file restriction type. Here I separate them with "|" to obtain arrays and verify them cyclically.
String ext = FileName. Substring (FileName. LastIndexOf (".") + 1, FileName. Length-FileName. LastIndexOf (".")-1 );
For (int I = 0; I <_ AcceptedFileTypes. Length; I ++)
{
If (ext = _ AcceptedFileTypes)
{
Return true;
}
}
Return false;
}

/// <Summary>
/// Get the file extension
/// </Summary>
/// <Param name = "fileName"> </param>
/// <Returns> </returns>
Private string GetExtension (string fileName)
{
Try
{
Int startpos = filename. lastindexof (".");
String ext = filename. substring (startpos, filename. Length-startpos );
Return ext;
}
Catch
{
Return string. empty;
}
}

/// <Summary>
/// Path of the file to be saved
/// </Summary>
/// <Param name = "picpath"> file path </param>
/// <Returns> </returns>
Private string getsavefilepath (string picpath)
{
If (! Directory. exists (picpath ))
{
Directory. createdirectory (picpath );
}
Return picpath;
}

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.