Asp. NET upload file management strategy

Source: Internet
Author: User
Tags datetime file size tostring
Asp.net| Strategy | upload

Recently do the project with ASP.net upload file content, so the code posted, so that users can consult, to provide solutions to such problems: if there is no understanding of the problem, please leave a message, in time to help you solve!

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Login.aspx.cs" inherits= "Login"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title&gt:::: Uploading Management system::::</title>
<body>
<form id= "Form1" runat= "Server" >
<div style= "border-right: #ffff00 thin dashed; Border-top: #ffff00 thin dashed; margin-bottom:4px; margin-left:55px; Border-left: #ffff00 thin dashed; Line-height:normal; margin-right:55px; padding-top:1px; Border-bottom: #ffff00 thin dashed; Letter-spacing:normal; position:static; Background-color:silver; Text-align:center ">
<br/>
<asp:label id= "Labeltitle" runat= "Server" font-bold= "True" font-size= "Xx-large" forecolor= "BLACK"
text= "Upload management" ></asp:label><br/>
<br/>
<asp:fileupload id= "FileUpload1" runat= "Server" width= "369px"/>
<asp:button id= "Buttonup" runat= "Server" height= "21px" text= "Upload" width= "67px"/><br/>
<br/>
<asp:label id= "LabelTitle2" runat= "Server" forecolor= "Red" text= "* Upload file cannot exceed 2M" ></asp:label><br/>
<b> original filename: </b><span id = "FileName" runat= "server"/><br/>
<b> upload to server: </b><span id= "Savedir" runat= "Server"/><br/>
<b> thumbnail address: </b><span id = "Ssavedir" runat = "server"/><br/>
<b> file type: </b><span id = "FileType" runat = "server"/><br/>
<b> File Size: </b><span id= "filelength" runat = "server"/><br/>
<b> file Development name: </b><span id= "fileextention" runat= "Server"/><br/>
<b> Upload date: <br/><span id = "UpdateTime" runat = "server"/><br/>
Picture preview: <br/>
<asp:image id= "Image1" runat= "Server" imageurl= "~/login.aspx" bordercolor= "#0000C0"/><br/>
</b>
<br/>
<br/>
</div>
</form>
</body>

********************************************************

Using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;

The

using MWO. Model.info; The
using MWO. DAL. Info;
public partial class Login:System.Web.UI.Page
{
    protected void Page_Load (object sender, EventArgs e)
    {
     
   }
     protected void Buttonup_click (object sender, EventArgs e)
    {
    & nbsp;   if (FileUpload1.PostedFile.FileName!= null)
        {
            Try
             {
                  filename.innerhtml = Fileupload1.postedfile.filename;//getphotoname
                  updatetime.innerhtml = DateTime.Now.ToShortDateString ();//updatetime

#region Setphotosize
filelength.innerhtml = Countsize (FileUpload1.PostedFile.ContentLength);
if (FileUpload1.PostedFile.ContentLength > 1024 * 1024 * 2)
{
Response.Write (' <script>alert (' picture cannot exceed the specified size! ');</script> ");
}
Else
{
#region Setphotoformat
filetype.innerhtml = FileUpload1.PostedFile.ContentType;
fileextention.innerhtml = System.IO.Path.GetExtension (FileUpload1.PostedFile.FileName). ToUpper ();
String m_filename = "www.webjx.com-" + DateTime.Now.ToString ("Yyyy-mm-dd-hh-mm-ss-ffff"). Replace (".", "-") + fileextention.innerhtml;
String m_sfilename = "www.webjx.com-" + DateTime.Now.ToString ("Yyyy-mm-dd-hh-mm-ss-ffff"). Replace (".", "-") + fileextention.innerhtml;
if (fileextention.innerhtml = = ". JPG ")
{
String m_savepath = Server.MapPath ("./photo/") + m_filename;
savedir.innerhtml = M_savepath;

                    #region      Setsmallphoto
                      this. Makesmallimg (Fileupload1.postedfile, this. Server.MapPath ("./sphoto/") + M_sfilename, 118, 90);
                      string M_ssavepath = this. Server.MapPath ("./sphoto/") + m_sfilename;
                      ssavedir.innerhtml = M_ssavepath;
                    #endregion

FileUpload1.PostedFile.SaveAs (M_savepath);
Response.Write (' <script>alert (' picture file saved successfully! ');</script> ");

#region Savedatabasee
Photoinfo m_pinfo = new Photoinfo ();
M_pinfo.organization = "5173";
M_pinfo.picname = filename.innerhtml;
M_pinfo.servername = "Glsdb";
M_pinfo.cdate = DateTime.Now;
M_pinfo.exp = fileextention.innerhtml;
M_pinfo.flag = 0;
M_pinfo.gamenickname = "Bruce";//textbox control content
M_pinfo.moonstar = 0;
M_pinfo.showindex = 0;
M_PINFO.SPICHTTPADDR = M_ssavepath;
M_pinfo.type = 0;
M_pinfo.vote = 0;
M_pinfo.pichttpaddr=m_savepath;

Photodao M_pdao = new Photodao ();
M_pdao.insert (M_pinfo);
#endregion
}
Else
{
Response.Write ("<script>alert picture format is not correct, please select picture File!") ');</script> ");
}
#endregion
}
#endregion
}
catch (Exception m_ex)
{
Response. Write ("<script>alert" ("+m_ex.tostring () +");</script> ");
}
}
else if (FileUpload1.PostedFile.FileName = "")
{
Response.Write (' <script>alert (' upload file cannot be empty! ');</script> ");
}
}

#region
<summary>
Calculate file size function, size to byte
</summary>
<param name= "Size" > Initial file Size </param>
<returns></returns>
public string countsize (long Size)
{
String m_strsize = "";
Long factsize = 0; Factsize = Size;
if (factsize <= 1024)
M_strsize = factsize.tostring () + "Byte";
if (factsize>=1024&&factsize<=1048576)
M_strsize= (factsize/1024). ToString () + "K";
if (factsize >= 1048576 && factsize <= 10485760)
M_strsize = (factsize/1024/1024). ToString () + "M";
return m_strsize;
}
#endregion

#region Savesmallphoto
<summary>
High-definition thumbnail image algorithm
</summary>
<param name= "Postfile" > Picture file Object </param>
<param name= "saveimg" > source file to be saved as thumbnails </param>
<param name= "width" > Breadth </param>
<param name= "height" > Height </param>
public void Makesmallimg (System.Web.HttpPostedFile postfile, String saveimg, System.Double Width, System.Double Height)
{

Sourcephotoname
string m_originalfilename = Postfile.filename;
string m_strgoodfile = saveimg;

       //getphotoobject from SourceFile
         System.Drawing.Image m_image = System.Drawing.Image.FromStream (Postfile.inputstream, true);

        system.double newwidth, newheight;
        if (M_image.width > M_image.height)
         {
            newwidth = Width;
            newheight = m_image.height * (newwidth/m_ Image.width);
       }
        Else
        {
             newheight = Height;
            newwidth = (newheight/m_image.height) * m_ Image.width;
       }

if (Newwidth > Width)
{
Newwidth = Width;
}
if (Newheight > Height)
{
Newheight = Height;
}

Getphotosize
System.Drawing.Size Size =new System.Drawing.Size (int) newwidth, (int) newheight);
The New of Bimp Photo
System.Drawing.Image bitmap = new System.Drawing.Bitmap (size. Width, size. Height);
The New of Palette
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage (bitmap);
Set hightquality arithmetic for Graphics
G.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.High;
Set high quality, low speed rendering smooth degree
G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
Clearcanvas
G.clear (System.Drawing.Color.White);
Paint at a specified location
G.drawimage (M_image, New System.Drawing.Rectangle (0, 0, bitmap.) Width, Bitmap. Height),
New System.Drawing.Rectangle (0, 0, M_image.width, m_image.height),
System.Drawing.GraphicsUnit.Pixel);

Savephoto of Hightfocus
Bitmap. Save (M_strgoodfile, System.Drawing.Imaging.ImageFormat.Jpeg);
Disposeres
G.dispose ();
M_image.dispose ();
Bitmap. Dispose ();
}

#endregion
}



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.