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;
} |