asp.net fileupload class Implementation upload file instance _ practical skills

Source: Internet
Author: User
Tags rar

The example of this article describes the method of uploading files by ASP.net fileupload class. Share to everyone for your reference.

The specific function code is as follows:

Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Web.UI;
Using System.Web;
Using System.Web.UI.WebControls;
Using System.Collections;
Using System.Drawing;
Using System.Drawing.Imaging;
Using System.IO;

Namespace CSFRAMEWORK.BLL
{
<summary>
File types that support uploading
</summary>
public enum Uploadfiletype
{
Articleattachment = 1,
Image = 2,
Video = 3,
All = 4
}

<summary>
Upload file Management class
</summary>
public class Cfileupload
{
Private FileUpload _fileupload;
private string _savepath;
private string _lastuploadedfile = String. Empty;
private bool _autogenfilename = false;
private bool _autogenwatermark = false;
public string Lastuploadedfile {get {return _lastuploadedfile;}}
private string _error = "";

private string picture_file = "[. gif.png.jpeg.jpg]";
private string zip_file = "[. Zip.rar]";
private string muilt_media_file = "[. mpeg.mpg.fla.wma]";

private int img_max_width = 700;//specified width
private int img_max_height = 0;//unspecified height
private int max_size_upload = 1024;//maximum support for uploading files less than 1MB.

<summary>
Construction device
</summary>
<param name= "FileUpload" >asp.net FileUpload object </param>
<param name= "Savepath" > Save directory, does not contain file name </param>
<param name= "Autogenfilename" > automatically generate file name </param>
Public Cfileupload (FileUpload fileupload, string savepath, bool Autogenfilename, BOOL Autogenwatermark)
{
_savepath = Savepath;
_fileupload = FileUpload;
_autogenfilename = Autogenfilename;
_autogenwatermark = Autogenwatermark;
}

<summary>
Construction device
</summary>
<param name= "FileUpload" >asp.net FileUpload object </param>
<param name= "Savepath" > Save directory, does not contain file name </param>
Public Cfileupload (FileUpload fileupload, String savepath)
{
_savepath = Savepath;
_fileupload = FileUpload;
}

<summary>
Upload rar files
</summary>
public bool Uploadrarfile ()
{
Return Doupload (Zip_file);
}

<summary>
Uploading video files
</summary>
public bool Uploadvideo ()
{
Return Doupload (Muilt_media_file);
}

<summary>
Upload picture file
</summary>
public bool Uploadimage ()
{
Return Doupload (Picture_file);
}

public bool Uploadimage (int maxwidth, int maxheight)
{
This. Img_max_width = MaxWidth;
This. Img_max_height = MaxHeight;
Return Doupload (Picture_file);
}

<summary>
Upload any supported files
</summary>
public bool Uploadanysupported ()
{
Return Doupload (Picture_file zip_file muilt_media_file);
}

<summary>
Generate a new file name
</summary>
private string Getnewfilename (String folder, String fileName)
{
if (_autogenfilename)//automatically generates a 32-bit GUID file name
{
String ext = System.IO.Path.GetExtension (fileName);
String newfile = Guid.NewGuid (). ToString (). Replace ("-", "") ext;
Return folder NewFile;
}
Else
{
if (System.IO.File.Exists (folder FileName))
{
String ext = System.IO.Path.GetExtension (fileName);
String filebody = Filename.replace (ext, "");

int x = 1;
while (TRUE)///If file exists, generate tail-band (x) file
{
String newfile = Folder Filebody "(" X.tostring () ")" EXT;
if (! System.IO.File.Exists (NewFile))
Return folder Filebody "(" X.tostring () ")" EXT;
Else
x;
}
}
Else
Return folder FileName;
}
}

<summary>
Maximum support for files less than 1MB.
</summary>
private bool Allowmaxsize (int filelength)
{
Double kb = filelength/1024;
return (int) KB < Max_size_upload;
}

private bool Doupload (string allowedextensions)
{
Try
{
BOOL FileOK = false;

if (!_fileupload.hasfile) throw new Exception ("No file!") "); If you do not include a file in the upload control, exit

Get the suffix of the file
String fileextension = System.IO.Path.GetExtension (_fileupload.filename). ToLower ();

To see if the included file is a permitted file suffix
FileOK = Allowedextensions.indexof (fileextension) > 0;
if (!fileok) throw new Exception ("Unsupported file format!") ");

Check upload file size
FileOK = Allowmaxsize (_fileupload.filebytes.length);
if (!fileok) throw new Exception ("Picture file cannot be greater than" max_size_upload.) ToString () "kb! ");

Try
{
File exists in the server specified directory
String savefile = Getnewfilename (_savepath, _fileupload.filename);

if (Isuploadimage (fileextension))//Save Picture
{
System.Drawing.Image output = Cimagelibrary.frombytes (_fileupload.filebytes);

Check picture width/height/size
if (this. Img_max_width!= 0 && output. Width > this. Img_max_width)
{
Output = cimagelibrary.getoutputsizeimage (output, this. Img_max_width);
}

Bitmap bmp = new Bitmap (output);

This. Createdir (Path.getdirectoryname (savefile));

Bmp. Save (savefile, output. Rawformat);
Bmp. Dispose ();
Output. Dispose ();

if (_autogenwatermark)
{
Watermarkimage Genwatermark = new Watermarkimage ();
Genwatermark.drawwords (SaveFile, AppConfig.Current.WatermarkMain,
AppConfig.Current.WatermarkDesc, float. Parse ("0.2"));
}
}
else//any other documents
{
This. Createdir (Path.getdirectoryname (savefile));

_fileupload.postedfile.saveas (SaveFile);
}

_lastuploadedfile = SaveFile;

return true;
}
catch (Exception ex)
{
throw new Exception ("An unknown error occurred while uploading the file!") "Ex." message);
}
}
catch (Exception ex)
{
_error = ex. message;
return false;
}
}

private void Createdir (String dir)
{
if (directory.exists (dir) = = False)
Directory.CreateDirectory (dir);
}

private bool Isuploadimage (string fileextension)
{
BOOL Isimage = Picture_file. IndexOf (FileExtension) > 0;
return isimage;
}
}
}

I hope this article will help you with the ASP.net program design.

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.