SWFUpload Uploading Images

Source: Internet
Author: User

Front:

<script src= "~/scripts/swfupload/swfupload.js" ></script>
<script src= "~/scripts/swfupload/swfupload.queue.js" ></script>
<script src= "~/scripts/swfupload/swfupload.handlers.js" ></script>

<script type= "Text/javascript" >
$ (function () {

$ (". Uploadimg"). each (function () {
$ (this). Initswfupload ({type:1, btntext: "Uploading pictures", btnwidth:86, Btnheight:28, Single:false, Water:true, Thumbnail:true, Filesi Ze: "2048", Sendurl: "/pics/uploadfile", Flashurl: ". /.. /scripts/swfupload/swfupload.swf ", FileTypes:" *.jpg;*.jpge;*.png;*.gif; "});
});

});

</script>

<div class= "Uploadimg" ></div>

Background:

public class UpLoad
{
int imgmaxheight = 0;
int imgmaxwidth = 0;
int thumbnailwidth = 200;
int thumbnailheight = 200;
int imgsize = 10240;
int attachsize = 51200;
int watermarktype = 2;
int filesave = 2;
String webpath = "/";
string filepath = "Upload";
String fileextension = "Gif,jpg,png,bmp,rar,zip,doc,xls,txt";
String watermarktext = "Zyan";
int watermarkposition = 9;
int watermarkfontsize = 12;
String watermarkpic = "Watermark.png";
int watermarktransparency = 5;
int watermarkimgquality = 80;
String watermarkfont = "Zyan";
<summary>
Crop and save the picture
</summary>
public bool Cropsaveas (string fileName, string newfilename, int maxWidth, int. maxheight, int cropwidth, int cropheight, in T X, int Y)
{
String fileext = Utils.getfileext (fileName); File extension, not including "."
if (! Isimage (Fileext))
{
return false;
}
String newfiledir = Utils.getmappath (newfilename.substring (0, Newfilename.lastindexof (@ "/") + 1));
Check if there is a path, no then create
if (! Directory.Exists (Newfiledir))
{
Directory.CreateDirectory (Newfiledir);
}
Try
{
String filefullpath = Utils.getmappath (fileName);
String tofilefullpath = Utils.getmappath (NewFileName);
Return Thumbnail.makethumbnailimage (Filefullpath, Tofilefullpath, N/A, cropwidth, Cropheight, X, Y);
}
Catch
{
return false;
}
}

<summary>
File Upload Method
</summary>
<param name= "PostedFile" > File Flow </param>
<param name= "Isthumbnail" > whether to generate thumbnails </param>
<param name= "Iswater" > Whether water seal </param>
<returns> Post-upload file information </returns>
public string FileSaveAs (HttpPostedFileBase postedFile, bool isthumbnail, bool iswater,string type)
{
Try
{
String fileext = Utils.getfileext (postedfile.filename); File extension, not including "."
int fileSize = Postedfile.contentlength; Gets the file size, in bytes
String fileName = postedFile.FileName.Substring (postedFile.FileName.LastIndexOf (@ "\") + 1); Get the original file name
String newfilename = Utils.getramcode () + "." + Fileext; Randomly generate a new file name
String newthumbnailfilename = "Thumb_" + newfilename; Randomly generated thumbnail file names
String uploadpath = Getuploadpath (); Upload Directory relative path
String fulluploadpath = Utils.getmappath (Uploadpath); The physical path of the upload directory
String Newfilepath = Uploadpath + newfilename; Post-upload path
String Newthumbnailpath = Uploadpath + newthumbnailfilename; Thumbnail path after uploading

Check that the file name extension is legitimate
if (! Checkfileext (Fileext))
{
Return "{\" status\ ": 0, \" msg\ ": \" Do not allow uploading "+ Fileext +" type of file! \"}";
}
Check file size is legitimate
if (! Checkfilesize (Fileext, fileSize))
{
Return "{\" status\ ": 0, \" msg\ ": \" file exceeds the limit size! \"}";
}
Check if the physical path of the upload exists and does not exist create
if (! Directory.Exists (Fulluploadpath))
{
Directory.CreateDirectory (Fulluploadpath);
}

Save File
Postedfile.saveas (Fulluploadpath + newfilename);
If it is a picture, check if the picture exceeds the maximum size, then crop
if (Isimage (fileext) && (imgmaxheight > 0 | | imgmaxwidth > 0))
{
Thumbnail.makethumbnailimage (Fulluploadpath + newfilename, Fulluploadpath + NewFileName,
Imgmaxwidth, Imgmaxheight);
}
If it is a picture, check whether you need to generate a thumbnail, or generate
if (Isimage (fileext) && isthumbnail && thumbnailwidth > 0 && thumbnailheight > 0)
{
Thumbnail.makethumbnailimage (Fulluploadpath + newfilename, Fulluploadpath + newthumbnailfilename,
Thumbnailwidth, Thumbnailheight, "Cut");
}
If it's a picture, check if you need a water stamp.
if (Iswatermark (fileext) && iswater)
{
Switch (watermarktype)
{
Case 1:
Watermark.addimagesigntext (Newfilepath, Newfilepath,
Watermarktext, Watermarkposition,
Watermarkimgquality, Watermarkfont, watermarkfontsize);
Break
Case 2:
Watermark.addimagesignpic (Newfilepath, Newfilepath,
Watermarkpic, Watermarkposition,
Watermarkimgquality, watermarktransparency);
Break
}
}
Finished processing, return file information in JOSN format
Return "{\" status\ ": 1, \" msg\ ": \" Upload the file successfully! \ ", \" name\ ": \" "
+ fileName + "\", \ "path\": \ "" + Newfilepath + "\", \ "thumb\": \ ""
+ Newthumbnailpath + "\", \ "size\": "+ fileSize +", \ "type\": "+ type +", \ "ext\": \ "+ Fileext +" \ "}";
}
Catch
{
Return "{\" status\ ": 0, \" msg\ ": \" An unexpected error occurred during the upload process! \"}";
}
}

#region Private Methods

<summary>
Returns the upload directory relative path
</summary>
<param name= "filename" > upload file name </param>
private String Getuploadpath ()
{
String path = Webpath + filepath + "/"; Site Directory + upload Directory
Switch (FileSave)
{
Case 1: One folder per day by month date
Path + = DateTime.Now.ToString ("YyyyMMdd");
Break
Default://By month/day in different folders
Path + = DateTime.Now.ToString ("yyyymm") + "/" + DateTime.Now.ToString ("DD");
Break
}
Return path + "/";
}

<summary>
Do you need water marks?
</summary>
<param name= "_fileext" > File extension, not including "." </param>
private bool Iswatermark (string _fileext)
{
Determine if the watermark is turned on
if (Watermarktype > 0)
{
The type of picture that determines whether a watermark can be struck
ArrayList al = new ArrayList ();
Al. ADD ("BMP");
Al. ADD ("JPEG");
Al. ADD ("JPG");
Al. ADD ("PNG");
if (al. Contains (_fileext.tolower ()))
{
return true;
}
}
return false;
}

<summary>
is the picture file
</summary>
<param name= "_fileext" > File extension, not including "." </param>
private bool Isimage (string _fileext)
{
ArrayList al = new ArrayList ();
Al. ADD ("BMP");
Al. ADD ("JPEG");
Al. ADD ("JPG");
Al. ADD ("GIF");
Al. ADD ("PNG");
if (al. Contains (_fileext.tolower ()))
{
return true;
}
return false;
}

//<summary>
//Check whether it is a valid upload file
//</summary>
private bool Checkfileext (string _fileext)
{
//Check dangerous file
string[] Excext = {"ASP", "aspx", "PHP", "jsp", "htm", "html", "JS", "EXE"};
for (int i = 0; I &l T Excext.length; i++)
{
if (excext[i]. ToLower () = = _fileext.tolower ())
{
return false;
}
}
//Check for legitimate files
string[] Allowext = fileextension. Split (', ');
for (int i = 0; i < allowext.length; i++)
{
if (allowext[i). ToLower () = = _fileext.tolower ())
{
return true;
}
}
return false;
}

<summary>
Check file size is legitimate
</summary>
<param name= "_fileext" > File extension, not including "." </param>
<param name= "_filesize" > File size (B) </param>
private bool Checkfilesize (string _fileext, int _filesize)
{
Decide whether to be a picture file
if (Isimage (_fileext))
{
if (imgsize > 0 && _filesize > imgsize * 1024)
{
return false;
}
}
Else
{
if (attachsize > 0 && _filesize > attachsize * 1024)
{
return false;
}
}
return true;
}

#endregion
}

public class Picscontroller

{

[HttpPost]
public string UploadFile (int iswater = 0, int isthumbnail = 0, string delfile = "", String type = "")
{

HttpPostedFileBase _upfile = httpcontext.request.files["Filedata"];
BOOL _iswater = false; Watermark is not hit by default
BOOL _isthumbnail = false; Thumbnails are not generated by default

if (Iswater = = 1)
_iswater = true;
if (Isthumbnail = = 1)
_isthumbnail = true;
if (_upfile = = null)
{
Return "{\" status\ ": 0, \" msg\ ": \" Please select the file to upload! \"}";
}
var upfiles = new UpLoad ();
String msg = Upfiles.filesaveas (_upfile, _isthumbnail, _iswater, type);
Delete old files that already exist
if (!string. IsNullOrEmpty (Delfile))
{
Utils.deleteupfile (Delfile);
}
return msg;
}

}

SWFUpload Uploading Images

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.