{"id":135,"date":"2008-12-16T11:04:14","date_gmt":"2008-12-16T11:04:14","guid":{"rendered":""},"modified":"2008-12-16T11:04:14","modified_gmt":"2008-12-16T11:04:14","slug":"135","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=135","title":{"rendered":"C#\u5b9e\u73b0Web\u6587\u4ef6\u4e0a\u4f20\u7684\u4e24\u79cd\u65b9\u6cd5"},"content":{"rendered":"<p><code><br \/>using System;<br \/>using System.Data;<br \/>using System.Data.SqlClient;<br \/>using System.Web.UI.HtmlControls;<br \/>using System.Drawing.Imaging;<br \/>using System.Configuration;<br \/>using System.Drawing;<\/p>\n<p>namespace zhuanti<br \/>{<br \/>\/\/\/ &lt;summary&gt;<br \/>\/\/\/ \u8fd9\u662f\u4e00\u4e2a\u7528\u4e8e\u73a9\u5bb6\u6295\u7a3f\u4e2d\u5b9e\u73b0\u73a9\u5bb6\u4e0a\u4f20\u6587\u4ef6\u529f\u80fd\u4e2d\u7528\u5230\u7684\u76f8\u5e94\u7684\u51fd\u6570\u7684\u529f\u80fd\u6a21\u5757<br \/>\/\/\/ &lt;\/summary&gt;<br \/>public class FileUpload<br \/>{<br \/>public enum File \/\/\u5b9a\u4e49\u4e00\u4e2a\u4eba\u7528\u4e8e\u5b58\u653e\u73a9\u5bb6\u4e0a\u4f20\u6587\u4ef6\u4fe1\u606f\u7684\u4e00\u4e2a\u6570\u7ec4<br \/>{<br \/>FILE_SIZE , \/\/\u5927\u5c0f<br \/>FILE_POSTNAME, \/\/\u7c7b\u578b\uff08\u6587\u4ef6\u540e\u7f00\u540d\uff09<br \/>FILE_SYSNAME , \/\/\u7cfb\u7edf\u540d<br \/>FILE_ORGINNAME, \/\/\u539f\u6765\u7684\u540d\u5b57<br \/>FILE_PATH \/\/\u6587\u4ef6\u8def\u5f84<br \/>}<br \/>private static Random rnd = new Random(); \/\/\u83b7\u53d6\u4e00\u4e2a\u968f\u673a\u6570<\/p>\n<p>public static string[] UploadFile(HtmlInputFile file,string Upload_Dir) \/\/\u5b9e\u73b0\u73a9\u5bb6\u6587\u4ef6\u4e0a\u4f20\u529f\u80fd\u7684\u4e3b\u51fd\u6570<br \/>{<br \/>string[] arr = new String[5];<br \/>string FileName = GetUniquelyString(); \/\/\u83b7\u53d6\u4e00\u4e2a\u4e0d\u91cd\u590d\u7684\u6587\u4ef6\u540d<br \/>string FileOrginName = file.PostedFile.FileName.Substring<\/p>\n<p>(file.PostedFile.FileName.LastIndexOf(\"\\\")+1);\/\/\u83b7\u53d6\u6587\u4ef6\u7684\u539f\u59cb\u540d<br \/>if(file.PostedFile.ContentLength&lt;=0)<br \/>{ return null; }<br \/>string postFileName;<br \/>string FilePath = Upload_Dir.ToString();<br \/>string path = FilePath + \"\\\";<br \/>try<br \/>{<br \/>int pos = file.PostedFile.FileName.LastIndexOf(\".\")+1;<br \/>postFileName = file.PostedFile.FileName.Substring(pos,file.PostedFile.FileName.Length-pos);<br \/>file.PostedFile.SaveAs(path+FileName+\".\"+postFileName); \/\/\u5b58\u50a8\u6307\u5b9a\u7684\u6587\u4ef6\u5230\u6307\u5b9a\u7684\u76ee\u5f55<br \/>}<br \/>catch(Exception exec)<br \/>{<br \/>throw(exec);<br \/>}<\/p>\n<p>double unit = 1024;<br \/>double size = Math.Round(file.PostedFile.ContentLength\/unit,2);<br \/>arr[(int)File.FILE_SIZE] = size.ToString(); \/\/\u6587\u4ef6\u5927\u5c0f<br \/>arr[(int)File.FILE_POSTNAME] = postFileName; \/\/\u6587\u4ef6\u7c7b\u578b\uff08\u6587\u4ef6\u540e\u7f00\u540d\uff09<br \/>arr[(int)File.FILE_SYSNAME] = FileName; \/\/\u6587\u4ef6\u7cfb\u7edf\u540d<br \/>arr[(int)File.FILE_ORGINNAME] = FileOrginName; \/\/\u6587\u4ef6\u539f\u6765\u7684\u540d\u5b57<br \/>arr[(int)File.FILE_PATH]=path+FileName+\".\"+postFileName; \/\/\u6587\u4ef6\u8def\u5f84<br \/>return arr;<br \/>}<\/p>\n<p>public static bool OperateDB(string sqlstr) \/\/\u5efa\u7acb\u4e00\u4e2a\u548c\u6570\u636e\u5e93\u7684\u5173\u8054<br \/>{<br \/>if (sqlstr==String.Empty)<br \/>return false;<\/p>\n<p>SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings[\"connstring\"]);<br \/>SqlCommand myCommand = new SqlCommand(sqlstr, myConnection);<\/p>\n<p>myConnection.Open();<br \/>myCommand.ExecuteNonQuery();<br \/>myConnection.Close();<br \/>return true;<br \/>}<\/p>\n<p>public static string GetUniquelyString() \/\/\u83b7\u53d6\u4e00\u4e2a\u4e0d\u91cd\u590d\u7684\u6587\u4ef6\u540d<br \/>{<br \/>const int RANDOM_MAX_VALUE = 1000;<br \/>string strTemp,strYear,strMonth,strDay,strHour,strMinute,strSecond,strMillisecond;<\/p>\n<p>DateTime dt =DateTime.Now;<br \/>int rndNumber = rnd.Next(RANDOM_MAX_VALUE);<br \/>strYear = dt.Year.ToString ();<br \/>strMonth = (dt.Month &gt; 9)? dt.Month.ToString() : \"0\" + dt.Month.ToString();<br \/>strDay = (dt.Day &gt; 9)? dt.Day.ToString() : \"0\" + dt.Day.ToString();<br \/>strHour = (dt.Hour &gt; 9)? dt.Hour.ToString() : \"0\" + dt.Hour.ToString();<br \/>strMinute = (dt.Minute &gt; 9)? dt.Minute.ToString() : \"0\" + dt.Minute.ToString();<br \/>strSecond = (dt.Second &gt; 9)? dt.Second.ToString() : \"0\" + dt.Second.ToString();<br \/>strMillisecond = dt.Millisecond.ToString();<\/p>\n<p>strTemp = strYear + strMonth + strDay +\"_\"+ strHour + strMinute + strSecond +\"_\"+ strMillisecond +\"_\"+ rndNumber.ToString () ;<\/p>\n<p>return strTemp;<br \/>}<br \/>}<br \/>}<br \/><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>using System;using System.Data;usi&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-135","post","type-post","status-publish","format-standard","hentry","category-NET"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=135"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/135\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}