Asp. NET upload files and log them to the database

Source: Internet
Author: User

Asp. NET upload files and log them to the database(2011-07-19 11:02:07) reproduced
Tags: gossip Category: ASP. NET Learning

Using System;
Using System.Data;
Using System.Configuration;
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;
Using System.Data.SqlClient;
Using System.IO;

public partial class _default:system.web.ui.page
{
protectedvoid Page_Load (object sender, EventArgs e)
{

}
Protectedvoid Button1_Click (object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName = = string. Empty)
{
Response.Write ("<script>alert (' Please select the file to upload! ');</script> ");
}
Else
{
Get information about a file to upload
string filepath = fileupload1.postedfile.filename;//file path
String Oldfilename =filepath. Substring (filepath. LastIndexOf ("\ \") +1);//File name
String fileextension = Path.getextension (oldfilename);//File extension
int filelength = (int) oldfilename. length;//filename Length
Randomly generated file names
Random Rnd = new Random ();
int strrnd = Rnd.next (1, 99);
String newfilename = DateTime.Now.Year.ToString () +datetime.now.month.tostring () + DateTime.Now.Day.ToString () + DateTime.Now.Hour.ToString () + DateTime.Now.Minute.ToString () +datetime.now.second.tostring () + strrnd.tostring () + Fileextension.tolower ();
Upload the file to a folder named after the current date
String Uploadname =datetime.now.year.tostring () + "-" +datetime.now.month.tostring () + "-" +datetime.now.day.tostring ( );
BOOL Fileuploadpathexists =file.exists (Server.MapPath ("UpLoad" + "\ \" +uploadname));
The specified folder does not exist and the folder is created if it does not exist
if (! fileuploadpathexists)
{
Directory.CreateDirectory (Server.MapPath ("UpLoad" + "\ \" +uploadname));
}
Save the uploaded file
String savapath = Server.MapPath ("UpLoad" + "\ \" +uploadname);//save path
String savapath1 = "UpLoad" + "\ \" + uploadname + "\ \" +newfilename;//Save path
FileUpload1.PostedFile.SaveAs (Savapath + "\" + NewFileName);
Connection Database string
string connstr = @ "datasource=.\sqlexpress; Attachdbfilename=d:\web_test\website4\app_data\database.mdf;integratedsecurity=true; User Instance=true ";
SqlConnection conn = new SqlConnection (CONNSTR);
Conn. Open ();
String cmdtext = "Insert Intot_upfile (Filename,filelength,filetype,filepath) VALUES ('" +newfilename + "', '" + filelength + "', '" + fileextension + "', '" +savapath1+ "')";
SqlCommand cmd = new SqlCommand (CMDTEXT, conn);
Try
{
Cmd. ExecuteNonQuery ();
System.Text.StringBuilder STRMSG = NewSystem.Text.StringBuilder ();
Strmsg.append ("<fontcolor=green> file was successfully added to the database, detailed information is shown below:<br>");
Strmsg.append ("The type of file uploaded is:" +this. FileUpload1.PostedFile.ContentType.ToString () + "<br>");
Strmsg.append ("Client file address is:" + filepath + "<br>");
Strmsg.append ("The file name of the uploaded file is:" + newfilename + "<br>");
Strmsg.append ("File upload to Server path:" + Savapath + "<br>");
Strmsg.append ("Upload file extension:" + fileextension + "<br>");
Strmsg.append ("Upload file Size:" + FileUpload1.PostedFile.ContentLength + "bytes </font>");
This. Label1.Text = Strmsg.tostring ();
}
catch (Exception error)
{
Response.Write (Error. ToString ());
}
Finally
{
Conn. Close ();
}
}
}
}
Database design

Create T_upfile

(

FileID int Identity (100000,1) primary key,

FileName varchar (50),

Filelength int,

FileType varchar (20),

FilePath varchar (MAX),

)

Asp. NET upload files and log them to the database

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.