web| Upload
C # Implementation of Web file upload-1
Using System;
Using System.Data;
Using System.Data.SqlClient;
Using System.Web.UI.HtmlControls;
Using System.Drawing.Imaging;
Using System.Configuration;
Using System.Drawing;
Namespace Zhuanti
{
<summary>
This is a functional module for player contributors to implement the corresponding functions used in the upload file function.
</summary>
public class FileUpload
{
public enum File//defines an array that a person uses to store information about a player uploading files
{
File_size,//Size
File_postname,//type (file suffix name)
File_sysname,//system name
File_orginname,//original name
File_path//file path
}
private static Random rnd = new Random (); Get a random number
public static string[] UploadFile (htmlinputfile file,string Upload_dir)//implement player file upload work
The main function of energy
{
string[] arr = new STRING[5];
String FileName = Getuniquelystring (); Get a file name that is not duplicated
String fileorginname = file. PostedFile.FileName.Substring
(file. PostedFile.FileName.LastIndexOf ("\") +1);//Get the original name of the file
if (file. postedfile.contentlength<=0)
{return null;}
String Postfilename;
String FilePath = Upload_dir.tostring ();
String path = FilePath + "\";
Try
{
int pos = file. PostedFile.FileName.LastIndexOf (".") +1;
Postfilename = file. PostedFile.FileName.Substring (Pos,file. Postedfile.filename.length-pos);
File. Postedfile.saveas (path+filename+ ".") +postfilename); Stores the specified file to the specified directory
}
catch (Exception exec)
{
throw (exec);
}
Double unit = 1024;
Double size = Math.Round (file. postedfile.contentlength/unit,2);
arr[(int) file.file_size] = SIZE. ToString (); File size
arr[(int) file.file_postname] = Postfilename; File type (file suffix name)
arr[(int) file.file_sysname] = FileName; File system name
arr[(int) file.file_orginname] = Fileorginname; The original name of the file
arr[(int) file.file_path]=path+filename+ "." +postfilename; File path
return arr;
}
public static bool Operatedb (string sqlstr)//Establish an association with the database
{
if (sqlstr==string.empty)
return false;
SqlConnection myconnection = new SqlConnection (configurationsettings.appsettings
["ConnString"]);
SqlCommand mycommand = new SqlCommand (sqlstr, MyConnection);
Myconnection.open ();
Mycommand.executenonquery ();
Myconnection.close ();
return true;
}
public static string getuniquelystring ()//Get a duplicate filename
{
const int random_max_value = 1000;
String Strtemp,stryear,strmonth,strday,strhour,strminute,strsecond,strmillisecond;
DateTime DT =datetime.now;
int rndnumber = rnd. Next (Random_max_value);
stryear = dt. Year.tostring ();
Strmonth = (dt. Month > 9)? Dt. Month.tostring (): "0" + dt. Month.tostring ();
Strday = (dt. Day > 9)? Dt. Day.tostring (): "0" + dt. Day.tostring ();
Strhour = (dt. Hour > 9)? Dt. Hour.tostring (): "0" + dt. Hour.tostring ();
Strminute = (dt. Minute > 9)? Dt. Minute.tostring (): "0" + dt. Minute.tostring ();
Strsecond = (dt. Second > 9)? Dt. Second.tostring (): "0" + dt. Second.tostring ();
Strmillisecond = Dt.Millisecond.ToString ();
strtemp = stryear + strmonth + strday + "_" + Strhour + strminute + Strsecond + "_" +
Strmillisecond + "_" + rndnumber.tostring ();
return strtemp;
}
}
}
C # Implementation of Web file upload-2
In web programming, we often need to upload some local files to the Web server, after uploading, users can easily browse through the browser
Some documents are very widely used.
So how do you use C # to implement file upload functions? The following is a brief introduction.
First, add a Web Form for uploading in your Visual C # Web project, and in order to upload files, you need to select the Toolbox
Select the HTML class's File field control to add the control to the Web form, but at this point the control is not a server-side control and we need to
It adds the following code: <input id=uploadfile1 type=file size=49 runat= "Server" so that it becomes a service
Control, and if you need to upload several files at the same time, we can add this control accordingly.
It is important to note that the code must set the property of <form> to be:
<form method=post enctype=multipart/form-data runat= "Server" >
Without this attribute, uploads cannot be implemented.
Then add a button for the Web Form class in this Web Form and double click the button for the following code:
Upload a picture of the program section
DateTime now = DateTime.Now;
Take the current time to the object in the Datatime class now
String strbaselocation = "d:\\web\\fc\\pic\\";
This is the absolute directory of the server to which the file will be uploaded
if (uploadfile1. Postedfile.contentlength!= 0)//Determine if the selection dialog box chooses a file length of 0
{Uploadfile1. Postedfile.saveas (Strbaselocation+now. Dayofyear.tostring ()
+uploadfile1. PostedFile.ContentLength.ToString () + ". jpg");
Perform uploads and automatically name files based on date and file size to ensure that they are not duplicated
label1.text= "Picture 1 has been uploaded, the file name is:" +now. Dayofyear.tostring ()
+uploadfile1. PostedFile.ContentLength.ToString () + ". jpg";
Navigator. Insert (System.Xml.TreePosition.After, XmlNodeType.Element, "Pic1", "", "");
Navigator. Insert (System.Xml.TreePosition.FirstChild, XmlNodeType.Text, "Pic1", "", "");
Navigator. Value= now. Dayofyear.tostring () +uploadfile1. PostedFile.ContentLength.ToString ()
+ ". jpg";
Navigator. Movetoparent ();}
The above code for the author developed a use of XML files to store news information System, the next few lines of code to write upload file information
to the XML file. If you want to upload other types of files, just need to change JPG to the appropriate type of suffix name, such as Doc can be uploaded
Word file, the browser can directly browse the uploaded Word file.
"Precautions"
1. Upload file can not be unlimited;
2. Be aware of the security aspects of IIS coordination;
3. When installing programs with visual Studio installations, be aware of the absolute path problem of the installer;
4. Note the name of the file upload after the problem.
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