1.  
  1. @{
  2. ViewBag.Title = "Index";
  3. Layout = null;
  4. }
  5.  
  6. <h2>Index</h2>
  7. <script src="~/Scripts/ajaxfileupload.js"></script>
  8.  
  9. <script src="~/Scripts/jquery.js"></script>
  10. <script src="~/Scripts/ajaxfileupload.js"></script>
  11. <script type="text/javascript">
  12.  
  13. $(document).ready(function () {
  14.  
  15. // $("#buttonUpload").click(function () {
  16. // alert("123");
  17. // $.ajaxFileUpload
  18. //(
  19. // {
  20. // url: '/Test/Check',
  21. // secureuri: false,
  22. // fileElementId: 'fileToUpload',
  23. // dataType: 'json',
  24. // data: null,
  25. // success: function (data, status) {
  26. // alert(data);
  27. // },
  28. // error: function (data, status, e) {
  29. // alert(e);
  30. // }
  31. // }
  32. //)
  33. // });
  34.  
  35. $("#buttonUpload").click(function () {
  36.  
  37. //$.post("/Test/Check", null, function (data) {
  38. // alert(data);
  39.  
  40. //});
  41. $.ajaxFileUpload(
  42. {
  43. url: '/Test/Check',
  44. secureuri: false,
  45. fileElementId: 'fileToUpload',
  46. dataType: 'json',
  47. data: null,
  48. success: function (data, status) {
  49. alert(data);
  50. },
  51. error: function (data, status, e) {
  52. alert(e);
  53. }
  54. }
  55. );
  56.  
  57. });
  58.  
  59. });
  60. </script>
  61.  
  62. <div>
  63. @*@Html.BeginForm("Index", "Test", FormMethod.Post, new { @enctype = "multipart/form-data" })
  64. {*@
  65. <div>
  66. <input id="fileToUpload" type="file" size="45" name="fileToUpload" class="input">
  67. </div>
  68. <div>
  69. <button class="button" id="buttonUpload">Upload</button>
  70. </div>
  71.  
  72. @* }*@
  73. </div>
  1.  
  1. jQuery.extend({
  2.  
  3. createUploadIframe: function(id, uri)
  4. {
  5. //create frame
  6. var frameId = 'jUploadFrame' + id;
  7. var iframeHtml = '<iframe id="' + frameId + '" name="' + frameId + '" style="position:absolute; top:-9999px; left:-9999px"';
  8. if(window.ActiveXObject)
  9. {
  10. if(typeof uri== 'boolean'){
  11. iframeHtml += ' src="' + 'javascript:false' + '"';
  12.  
  13. }
  14. else if(typeof uri== 'string'){
  15. iframeHtml += ' src="' + uri + '"';
  16.  
  17. }
  18. }
  19. iframeHtml += ' />';
  20. jQuery(iframeHtml).appendTo(document.body);
  21.  
  22. return jQuery('#' + frameId).get(0);
  23. },
  24. createUploadForm: function(id, fileElementId, data)
  25. {
  26. //create form
  27. var formId = 'jUploadForm' + id;
  28. var fileId = 'jUploadFile' + id;
  29. var form = jQuery('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');
  30. if(data)
  31. {
  32. for(var i in data)
  33. {
  34. jQuery('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form);
  35. }
  36. }
  37. var oldElement = jQuery('#' + fileElementId);
  38. var newElement = jQuery(oldElement).clone();
  39. jQuery(oldElement).attr('id', fileId);
  40. jQuery(oldElement).before(newElement);
  41. jQuery(oldElement).appendTo(form);
  42.  
  43. //set attributes
  44. jQuery(form).css('position', 'absolute');
  45. jQuery(form).css('top', '-1200px');
  46. jQuery(form).css('left', '-1200px');
  47. jQuery(form).appendTo('body');
  48. return form;
  49. },
  50.  
  51. ajaxFileUpload: function(s) {
  52. // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
  53. s = jQuery.extend({}, jQuery.ajaxSettings, s);
  54. var id = new Date().getTime()
  55. var form = jQuery.createUploadForm(id, s.fileElementId, (typeof(s.data)=='undefined'?false:s.data));
  56. var io = jQuery.createUploadIframe(id, s.secureuri);
  57. var frameId = 'jUploadFrame' + id;
  58. var formId = 'jUploadForm' + id;
  59. // Watch for a new set of requests
  60. if ( s.global && ! jQuery.active++ )
  61. {
  62. jQuery.event.trigger( "ajaxStart" );
  63. }
  64. var requestDone = false;
  65. // Create the request object
  66. var xml = {}
  67. if ( s.global )
  68. jQuery.event.trigger("ajaxSend", [xml, s]);
  69. // Wait for a response to come back
  70. var uploadCallback = function(isTimeout)
  71. {
  72. var io = document.getElementById(frameId);
  73. try
  74. {
  75. if(io.contentWindow)
  76. {
  77. xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
  78. xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
  79.  
  80. }else if(io.contentDocument)
  81. {
  82. xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
  83. xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
  84. }
  85. }catch(e)
  86. {
  87. jQuery.handleError(s, xml, null, e);
  88. }
  89. if ( xml || isTimeout == "timeout")
  90. {
  91. requestDone = true;
  92. var status;
  93. try {
  94. status = isTimeout != "timeout" ? "success" : "error";
  95. // Make sure that the request was successful or notmodified
  96. if ( status != "error" )
  97. {
  98. // process the data (runs the xml through httpData regardless of callback)
  99. var data = jQuery.uploadHttpData( xml, s.dataType );
  100. // If a local callback was specified, fire it and pass it the data
  101. if ( s.success )
  102. s.success( data, status );
  103.  
  104. // Fire the global callback
  105. if( s.global )
  106. jQuery.event.trigger( "ajaxSuccess", [xml, s] );
  107. } else
  108. jQuery.handleError(s, xml, status);
  109. } catch(e)
  110. {
  111. status = "error";
  112. jQuery.handleError(s, xml, status, e);
  113. }
  114.  
  115. // The request was completed
  116. if( s.global )
  117. jQuery.event.trigger( "ajaxComplete", [xml, s] );
  118.  
  119. // Handle the global AJAX counter
  120. if ( s.global && ! --jQuery.active )
  121. jQuery.event.trigger( "ajaxStop" );
  122.  
  123. // Process result
  124. if ( s.complete )
  125. s.complete(xml, status);
  126.  
  127. jQuery(io).unbind()
  128.  
  129. setTimeout(function()
  130. { try
  131. {
  132. jQuery(io).remove();
  133. jQuery(form).remove();
  134.  
  135. } catch(e)
  136. {
  137. jQuery.handleError(s, xml, null, e);
  138. }
  139.  
  140. }, 100)
  141.  
  142. xml = null
  143.  
  144. }
  145. }
  146. // Timeout checker
  147. if ( s.timeout > 0 )
  148. {
  149. setTimeout(function(){
  150. // Check to see if the request is still happening
  151. if( !requestDone ) uploadCallback( "timeout" );
  152. }, s.timeout);
  153. }
  154. try
  155. {
  156.  
  157. var form = jQuery('#' + formId);
  158. jQuery(form).attr('action', s.url);
  159. jQuery(form).attr('method', 'POST');
  160. jQuery(form).attr('target', frameId);
  161. if(form.encoding)
  162. {
  163. jQuery(form).attr('encoding', 'multipart/form-data');
  164. }
  165. else
  166. {
  167. jQuery(form).attr('enctype', 'multipart/form-data');
  168. }
  169. jQuery(form).submit();
  170.  
  171. } catch(e)
  172. {
  173. jQuery.handleError(s, xml, null, e);
  174. }
  175.  
  176. jQuery('#' + frameId).load(uploadCallback );
  177. return {abort: function () {}};
  178.  
  179. },
  180.  
  181. uploadHttpData: function( r, type ) {
  182. var data = !type;
  183. data = type == "xml" || data ? r.responseXML : r.responseText;
  184. // If the type is "script", eval it in global context
  185. if ( type == "script" )
  186. jQuery.globalEval( data );
  187. // Get the JavaScript object, if JSON is used.
  188. if ( type == "json" )
  189. eval( "data = " + data );
  190. // evaluate scripts within html
  191. if ( type == "html" )
  192. jQuery("<div>").html(data).evalScripts();
  193.  
  194. return data;
  195. }
  196. })

C#_ajax fileupload的更多相关文章

  1. .JavaWeb文件上传和FileUpload组件使用

    .JavaWeb文件上传 1.自定义上传 文件上传时的表单设计要符合文件提交的方式: 1.提交方式:post 2.表单中有文件上传的表单项:<input type="file" ...

  2. C#-WebForm-文件上传-FileUpload控件

    FileUpload - 选择文件,不能执行上传功能,通过点击按钮实现上传 默认选择类型为所有类型 //<上传>按钮 void Button1_Click(object sender, E ...

  3. FileUpload组件

    package com.itheima.servlet; import java.io.File; import java.io.FileOutputStream; import java.io.IO ...

  4. 如何实现修改FileUpload样式

    这里先隐藏FileUpload 然后用一个input button和一个text来模拟FileUpload 具体代码为 <asp:FileUpload ID="FileUpload1& ...

  5. 上传文件fileupload

    文件上传: 需要使用控件-fileupload 1.如何判断是否选中文件? FileUpload.FileName -  选中文件的文件名,如果长度不大于0,那么说明没选中任何文件 js - f.va ...

  6. fileupload图片预览功能

    FileUpload上传图片前首先预览一下 看看效果: 在专案中,创建aspx页面,拉上FileUpload控件一个Image,将用来预览上传时的图片. <%@ Page Language=&q ...

  7. textbox button 模拟fileupload

    方案一:  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.asp ...

  8. FileUpload 上传文件,并实现c#使用Renci.SshNet.dll实现SFTP文件传输

    fileupload上传文件和jquery的uplodify控件使用方法类似,对服务器控件不是很熟悉,记录一下. 主要是记录新接触的sftp文件上传.服务器环境下使用freesshd搭建好环境后,wi ...

  9. C# 自定义FileUpload控件

    摘要:ASP.NET自带的FileUpload控件会随着浏览器的不同,显示的样式也会发生改变,很不美观,为了提高用户体验度,所以我们会去自定义FileUpload控件 实现思路:用两个Button和T ...

随机推荐

  1. C#调用C++导出类(转)

    由于使用别人的Dll,导出的是一个实体类,在C#里封送很难,百度下,有个朋友回复一篇英文的,虽然不一定使用,但可以作为一个知识点,现把原文贴下: c#调用C++写的dll导出类,包含继承,重载等详细介 ...

  2. Android Application Project 工程目录下各个文件的意思

    (1) src:源文件,主要是完成java代码的编写 (2) gen:ADT即系统自动生成的JAVA文件(即源代码目录),程序员千万不要去修改 (3) gen->[Package Name]-& ...

  3. 【Spark学习】Apache Spark配置

    Spark版本:1.1.1 本文系从官方文档翻译而来,转载请尊重译者的工作,注明以下链接: http://www.cnblogs.com/zhangningbo/p/4137969.html Spar ...

  4. TopCoder SRM 605 DIV1

    604的题解还没有写出来呢.先上605的. 代码去practice房间找. 说思路. A: 贪心,对于每个类型的正值求和,如果没有正值就取最大值,按着求出的值排序,枚举选多少个类型. B: 很明显是d ...

  5. 在Windows上,如何卸载RabbitMQ服务

    打开运行->CMD->sc delete RabbitMQ 如果报错..... 打开运行->regedit 找到RabbitMQ节点,删掉即可.(右侧看到的都是启动服务时,需要的配置 ...

  6. 内核参数优化/etc/sysctl.conf

    net.nf_conntrack_max = 65536000net.netfilter.nf_conntrack_tcp_timeout_established = 1200net.ipv4.tcp ...

  7. C#类、静态类、静态变量,初始化执行顺序

    执行顺序: 类的静态变量 ↓ 类的静态构造函数 ↓ 类的普通变量 ↓ 基类的静态变量 ↓ 基类的静态构造函数 ↓ 基类的普通变量 ↓ 基类的构造函数 ↓ 类的构造函数

  8. [iOS UI进阶 - 3.2] 手势识别器UIGestureRecognizer

    A.系统提供的手势识别器   1.敲击手势 UITapGestureRecognizer numberOfTapsRequired: 敲击次数 numberOfTouchesRequired: 同时敲 ...

  9. OAuth2-Server-php

    Yii 有很多 extension 可以使用,在查看了 Yii 官网上提供的与 OAuth 相关的扩展后,发现了几个 OAuth2 的客户端扩展,但是并没有找到可以作为 OAuth2 Server 的 ...

  10. UVaLive 6625 Diagrams & Tableaux (状压DP 或者 DFS暴力)

    题意:给一个的格子图,有 n 行单元格,每行有a[i]个格子,要求往格子中填1~m的数字,要求每个数字大于等于左边的数字,大于上边的数字,问有多少种填充方法. 析:感觉像个DP,但是不会啊...就想暴 ...