用到的命名空间:
- using System.Data.SqlClient;
- using System.IO;
- using System.Xml;
- <script type="text/javascript" language="javascript">
- <!--
- function PreviewImg()
- {
- //原来的预览代码,不支持 IE7。
- var oldPreview = document.getElementById("newPreview");
- oldPreview.innerHTML = "<img src=\"" + document.getElementById("upload").value + "\" width=\"100%\" height=\"300px\" />";
- //新的预览代码,支持 IE6、IE7、IE8。
- var newPreview = document.getElementById("newPreview");
- newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = document.getElementById("upload").value;
- }
- //-->
- </script>
- <input id="upload" runat="server" name="upload" type="file" style=" 250px;
- font-size: 12px;" onchange="javascript:PreviewImg();" />
后台代码:
- protected void btnupload_ServerClick(object sender, EventArgs e)
- {
- string strFileName = "";
- XmlDocument document = new XmlDocument();
- document.Load(Server.MapPath("UploadFile.xml"));
- XmlNodeList cn = document.SelectNodes("fileset");
- string pathfile = cn.Item(0).SelectSingleNode("upload").Attributes["path"].Value.ToString();
- string strfileExt = cn.Item(0).SelectSingleNode("fileformat").InnerText.ToString();
- if (strfileExt.IndexOf(System.IO.Path.GetExtension(upload.PostedFile.FileName)) == -1)
- {
- Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script language='javascript'>alert('只支持.jpg文件上传。');</script>");
- return;
- }
- if ( selbanner.Value == "-2")
- {
- Page.ClientScript.RegisterStartupScript(Page.GetType(),"","<script>alert(\"请您选择所属频道!\");</script>");
- return;
- }
- using (scn = new SqlConnection())
- {
- scn.ConnectionString = connectionstring;
- scn.Open();
- using (scm = new SqlCommand())
- {
- scm = scn.CreateCommand();
- scm.CommandType = CommandType.StoredProcedure;
- scm.CommandText = "flash_upload_ADD";
- scm.Parameters.Add("@picdiscription", SqlDbType.VarChar, 50);
- scm.Parameters["@picdiscription"].Value = this.txtdiscription.Value;
- scm.Parameters.Add("@piclink", SqlDbType.VarChar, 500);
- scm.Parameters["@piclink"].Value = this.piclink.Value;
- scm.Parameters.Add("@picname", SqlDbType.VarChar, 50);
- strFileName = pathfile + "/" + System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Millisecond.ToString() + System.IO.Path.GetExtension(upload.PostedFile.FileName);
- scm.Parameters["@picname"].Value = strFileName;
- scm.Parameters.Add("@picorder", SqlDbType.Int);
- scm.Parameters["@picorder"].Value = orderid.Value;
- string bannerids = "";
- foreach(ListItem li in selbanner.Items)
- {
- if ( li.Selected == true)
- bannerids += li.Value + ",";
- }
- scm.Parameters.Add("@banner", SqlDbType.VarChar, 8000).Value = bannerids;
- scm.Parameters.Add("@information", SqlDbType.VarChar, 50);
- scm.Parameters["@information"].Direction = ParameterDirection.Output;
- scm.ExecuteNonQuery();
- Page.ClientScript.RegisterStartupScript(Page.GetType(),"","<script>alert('" + scm.Parameters["@information"].Value.ToString() + "');</script>");
- }
- }
- DirectoryInfo directoryinfo = new DirectoryInfo(Server.MapPath(pathfile));
- if (!directoryinfo.Exists)
- {
- directoryinfo.Create();
- }
- upload.PostedFile.SaveAs(Server.MapPath(strFileName));
- this.txtdiscription.Value = "";
- this.piclink.Value = "";
- }
Tags - 图片上传支持预览 , c# , asp.net