zoukankan      html  css  js  c++  java
  • 图片上传支持预览(C#) Fred

    用到的命名空间:

          
    1. using System.Data.SqlClient;     
    2. using System.IO;     
    3. using System.Xml;
          
    1. <script type="text/javascript" language="javascript">     
    2. <!--     
    3. function PreviewImg()     
    4. {     
    5. //原来的预览代码,不支持 IE7。     
    6. var oldPreview = document.getElementById("newPreview");     
    7. oldPreview.innerHTML = "<img src=\"" + document.getElementById("upload").value + "\" width=\"100%\" height=\"300px\" />";     
    8.     
    9. //新的预览代码,支持 IE6、IE7、IE8。     
    10. var newPreview = document.getElementById("newPreview");     
    11. newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = document.getElementById("upload").value;     
    12.     
    13. }     
    14. //-->     
    15. </script>     
    16.     
    17.     
    18.     
    19. <input id="upload" runat="server" name="upload" type="file" style=" 250px;     
    20. font-size: 12px;" onchange="javascript:PreviewImg();" />     

    后台代码:

          
    1. protected void btnupload_ServerClick(object sender, EventArgs e)     
    2. {     
    3. string strFileName = "";     
    4. XmlDocument document = new XmlDocument();     
    5. document.Load(Server.MapPath("UploadFile.xml"));     
    6. XmlNodeList cn = document.SelectNodes("fileset");     
    7. string pathfile = cn.Item(0).SelectSingleNode("upload").Attributes["path"].Value.ToString();     
    8. string strfileExt = cn.Item(0).SelectSingleNode("fileformat").InnerText.ToString();     
    9. if (strfileExt.IndexOf(System.IO.Path.GetExtension(upload.PostedFile.FileName)) == -1)     
    10. {     
    11. Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script language='javascript'>alert('只支持.jpg文件上传。');</script>");     
    12. return;     
    13. }     
    14. if ( selbanner.Value == "-2")     
    15. {     
    16. Page.ClientScript.RegisterStartupScript(Page.GetType(),"","<script>alert(\"请您选择所属频道!\");</script>");     
    17. return;     
    18. }     
    19. using (scn = new SqlConnection())     
    20. {     
    21. scn.ConnectionString = connectionstring;     
    22. scn.Open();     
    23. using (scm = new SqlCommand())     
    24. {     
    25. scm = scn.CreateCommand();     
    26. scm.CommandType = CommandType.StoredProcedure;     
    27. scm.CommandText = "flash_upload_ADD";     
    28.     
    29. scm.Parameters.Add("@picdiscription", SqlDbType.VarChar, 50);     
    30. scm.Parameters["@picdiscription"].Value = this.txtdiscription.Value;     
    31.     
    32. scm.Parameters.Add("@piclink", SqlDbType.VarChar, 500);     
    33. scm.Parameters["@piclink"].Value = this.piclink.Value;     
    34.     
    35. scm.Parameters.Add("@picname", SqlDbType.VarChar, 50);     
    36. 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);     
    37. scm.Parameters["@picname"].Value = strFileName;     
    38.     
    39. scm.Parameters.Add("@picorder", SqlDbType.Int);     
    40. scm.Parameters["@picorder"].Value = orderid.Value;     
    41.     
    42. string bannerids = "";     
    43. foreach(ListItem li in selbanner.Items)     
    44. {     
    45. if ( li.Selected == true)     
    46. bannerids += li.Value + ",";     
    47. }     
    48. scm.Parameters.Add("@banner", SqlDbType.VarChar, 8000).Value = bannerids;     
    49.     
    50. scm.Parameters.Add("@information", SqlDbType.VarChar, 50);     
    51. scm.Parameters["@information"].Direction = ParameterDirection.Output;     
    52.     
    53. scm.ExecuteNonQuery();     
    54.     
    55. Page.ClientScript.RegisterStartupScript(Page.GetType(),"","<script>alert('" + scm.Parameters["@information"].Value.ToString() + "');</script>");     
    56. }     
    57. }     
    58. DirectoryInfo directoryinfo = new DirectoryInfo(Server.MapPath(pathfile));     
    59. if (!directoryinfo.Exists)     
    60. {     
    61. directoryinfo.Create();     
    62. }     
    63. upload.PostedFile.SaveAs(Server.MapPath(strFileName));     
    64. this.txtdiscription.Value = "";     
    65. this.piclink.Value = "";     
    66. }

    Tags - 图片上传支持预览 , c# , asp.net
  • 相关阅读:
    npm包开发与发布
    mapbox展示动态图标
    axios并行请求
    Vue引入ES5的js库
    git常用操作
    单词倒排
    FT232RL芯片USB转TTL应用
    应用GL823芯片自制的读卡器
    队列图示
    队列
  • 原文地址:https://www.cnblogs.com/Fred_Xu/p/1446749.html
Copyright © 2011-2022 走看看