当前位置:Gxlcms > asp.net > 基于ASP.NET+easyUI框架实现图片上传功能(表单)

基于ASP.NET+easyUI框架实现图片上传功能(表单)

时间:2021-07-01 10:21:17 帮助过:10人阅读

基于ASP.Net +easyUI框架上传图片,实现图片上传,提交表单:

  1. <body>
  2. <link href="../../Easyui/themes/easyui.css" rel="stylesheet" type="text/css" />
  3. <script charset="utf-8" src="../../Easyui/jquery.easyui.min.js" type="text/javascript"></script>
  4. <script charset="utf-8" src="../../Easyui/easyui-lang-zh_CN.js" type="text/javascript"></script>
  5. <script charset="utf-8" src="../../Js/jquery.form.js" type="text/javascript"></script>
  6. <script type="text/javascript">
  7. //检查图片的格式是否正确,同时实现预览
  8. function setImagePreview(obj, localImagId, imgObjPreview) {
  9. var array = new Array('gif', 'jpeg', 'png', 'jpg', 'bmp'); //可以上传的文件类型
  10. if (obj.value == '') {
  11. $.messager.alert("让选择要上传的图片!");
  12. return false;
  13. }
  14. else {
  15. var fileContentType = obj.value.match(/^(.*)(\.)(.{1,8})$/)[3]; //这个文件类型正则很有用
  16. ////布尔型变量
  17. var isExists = false;
  18. //循环判断图片的格式是否正确
  19. for (var i in array) {
  20. if (fileContentType.toLowerCase() == array[i].toLowerCase()) {
  21. //图片格式正确之后,根据浏览器的不同设置图片的大小
  22. if (obj.files && obj.files[0]) {
  23. //火狐下,直接设img属性
  24. imgObjPreview.style.display = 'block';
  25. imgObjPreview.style.width = '200px';
  26. imgObjPreview.style.height = '150px';
  27. //火狐7以上版本不能用上面的getAsDataURL()方式获取,需要一下方式
  28. imgObjPreview.src = window.URL.createObjectURL(obj.files[0]);
  29. }
  30. else {
  31. //IE下,使用滤镜
  32. obj.select();
  33. var imgSrc = document.selection.createRange().text;
  34. //必须设置初始大小
  35. localImagId.style.width = "200px";
  36. localImagId.style.height = "150px";
  37. //图片异常的捕捉,防止用户修改后缀来伪造图片
  38. try {
  39. localImagId.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
  40. localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
  41. }
  42. catch (e) {
  43. $.messager.alert("您上传的图片格式不正确,请重新选择!");
  44. return false;
  45. }
  46. imgObjPreview.style.display = 'none';
  47. document.selection.empty();
  48. }
  49. isExists = true;
  50. return true;
  51. }
  52. }
  53. if (isExists == false) {
  54. $.messager.alert("上传图片类型不正确!");
  55. return false;
  56. }
  57. return false;
  58. }
  59. }
  60. //显示图片
  61. function over(imgid, obj, imgbig) {
  62. //大图显示的最大尺寸 4比3的大小 400 300
  63. maxwidth = 400;
  64. maxheight = 300;
  65. //显示
  66. obj.style.display = "";
  67. imgbig.src = imgid.src;
  68. //1、宽和高都超过了,看谁超过的多,谁超的多就将谁设置为最大值,其余策略按照2、3
  69. //2、如果宽超过了并且高没有超,设置宽为最大值
  70. //3、如果宽没超过并且高超过了,设置高为最大值
  71. if (img.width > maxwidth && img.height > maxheight) {
  72. pare = (img.width - maxwidth) - (img.height - maxheight);
  73. if (pare >= 0)
  74. img.width = maxwidth;
  75. else
  76. img.height = maxheight;
  77. }
  78. else if (img.width > maxwidth && img.height <= maxheight) {
  79. img.width = maxwidth;
  80. }
  81. else if (img.width <= maxwidth && img.height > maxheight) {
  82. img.height = maxheight;
  83. }
  84. };
  85. //保存信息
  86. function submitForm() {
  87. //先上传图片后,再提交
  88. upLoadFile();
  89. var test = document.getElementById("test").value = "add";
  90. var tbName = document.getElementById("tbName").value;
  91. var idFile = document.getElementById("idFile").value;
  92. //先判断是否上传图片之后在提交
  93. $('#ff').form('submit', {
  94. url: "../../Handler/add.ashx?tbName=" + tbName + "&idFile=" + idFile + "&test=" + test,
  95. dataType: "json",
  96. onSubmit: function () {
  97. if ($(this).form('validate'))
  98. return true;
  99. else {
  100. return false;
  101. }
  102. },
  103. success: function (data) {
  104. var dataJson = $.parseJSON(data);
  105. if (dataJson.success) {
  106. $("#add_address").dialog('destroy'); //销毁dialog对象
  107. $.messager.alert("提示", dataJson.msg)
  108. $("#dateList").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
  109. } else {
  110. $("#add_address").dialog('destroy'); //销毁dialog对象
  111. $.messager.alert("提示", dataJson.msg)
  112. }
  113. }
  114. });
  115. }
  116. //上传图片
  117. function upLoadFile() {
  118. var idFile = document.getElementById("idFile").value;
  119. //判断是否选择图片
  120. var options = {
  121. type: "POST",
  122. url: '../../Handler/InputImg.ashx'
  123. //success: showResponse
  124. };
  125. // 将options传给ajaxForm
  126. $('#ff').ajaxSubmit(options);
  127. }
  128. </script>
  129. <form id="ff" runat="server" method="post">
  130. <table style="width: 422px; margin-top: 20px; height: 91px;">
  131. <tr>
  132. <th style="text-align: right; width: 100px;" class="style1">
  133. 链接名称:
  134. </th>
  135. <td style="text-align: left" class="style1">
  136. <asp:TextBox ID="tbID" runat="server" Style="display: none"></asp:TextBox>
  137. <asp:TextBox ID="tbName" runat="server" Width="274px" Height="20px" class="easyui-validatebox"
  138. data-options="required:true"></asp:TextBox>
  139. </td>
  140. </tr>
  141. <tr>
  142. <th style="text-align: right; " class="style2">
  143. 链接logo:
  144. </th>
  145. <td class="style3">
  146. <div style="width: 307px; height: 22px;">
  147. 选择图片:<input id="idFile" style="width: 224px" runat="server" name="idFile" onchange="javascript:setImagePreview(this,localImag,preview);"
  148. type="file" />
  149. </div>
  150. <%--预 览:
  151. <div id="localImag">
  152. <img id="preview" onclick="over(preview,divImage,imgbig);" src="" style="width: 200px;
  153. height: 150px;" />
  154. </div>--%>
  155. </td>
  156. </tr>
  157. </table>
  158. <div style="width: 325px; text-align: center; margin-top: 20px; margin-left: 50px">
  159. <input type="hidden" id="test" name="test" />
  160. <a id="btn_sc" href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()">
  161. 上传</a>
  162. <a href="Friendly.aspx" class="easyui-linkbutton">取消</a>
  163. </div>
  164. </form>
  165. </body>

提交表单的一般处理程序: 

  1. BLL.J_Friendly frend = null;
  2. Model.J_Friendly fr = null;
  3. public void ProcessRequest(HttpContext context)
  4. {
  5. context.Response.ContentType = "text/plain";
  6. string command = context.Request["test"].ToString();//前台传的标示值
  7. if (command == "add")
  8. {
  9. AddFrend(context);
  10. }
  11. if (command == "update")
  12. {
  13. UpdateFrend(context);
  14. }
  15. }
  16. public void AddFrend(HttpContext context)
  17. {
  18. frend = new BLL.J_Friendly();
  19. fr = new Model.J_Friendly();
  20. string tbName = context.Request.QueryString["tbName"].Trim();
  21. if (frend.Exists("F_Name='" + tbName + "'"))
  22. {
  23. context.Response.Write("{\"msg\":\"添加失败,链接名称与已有的链接名称重复!\",\"success\":false}");
  24. return;
  25. }
  26. else
  27. {
  28. try
  29. {
  30. fr.F_Name = context.Request.QueryString["tbName"].Trim();
  31. }
  32. catch
  33. {
  34. context.Response.Write("{\"msg\":\"添加失败,请核对信息!\",\"success\":false}");
  35. return;
  36. }
  37. try
  38. {
  39. string img = context.Request.QueryString["idFile"].Trim();
  40. if (img == "")
  41. {
  42. context.Response.Write("{\"msg\":\"添加失败,请核对图片信息!\",\"success\":false}");
  43. return;
  44. }
  45. else
  46. {
  47. string str = context.Request.QueryString["idFile"].Trim();
  48. string str1 = str.Remove(0, str.LastIndexOf("\\") + 1);
  49. fr.F_Img = "../../Upload/Images/" + str1;
  50. }
  51. }
  52. catch
  53. {
  54. context.Response.Write("{\"msg\":\"添加失败,请核对信息!\",\"success\":false}");
  55. return;
  56. }
  57. }
  58. if (frend.Add(fr) > 0)
  59. {
  60. context.Response.Write("{\"msg\":\"添加成功!\",\"success\":true}");
  61. }
  62. else
  63. {
  64. context.Response.Write("{\"msg\":\"添加失败,请核对信息!\",\"success\":false}");
  65. }
  66. }

原型图:

 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

人气教程排行