C#_ajax fileupload
- @{
- ViewBag.Title = "Index";
- Layout = null;
- }
- <h2>Index</h2>
- <script src="~/Scripts/ajaxfileupload.js"></script>
- <script src="~/Scripts/jquery.js"></script>
- <script src="~/Scripts/ajaxfileupload.js"></script>
- <script type="text/javascript">
- $(document).ready(function () {
- // $("#buttonUpload").click(function () {
- // alert("123");
- // $.ajaxFileUpload
- //(
- // {
- // url: '/Test/Check',
- // secureuri: false,
- // fileElementId: 'fileToUpload',
- // dataType: 'json',
- // data: null,
- // success: function (data, status) {
- // alert(data);
- // },
- // error: function (data, status, e) {
- // alert(e);
- // }
- // }
- //)
- // });
- $("#buttonUpload").click(function () {
- //$.post("/Test/Check", null, function (data) {
- // alert(data);
- //});
- $.ajaxFileUpload(
- {
- url: '/Test/Check',
- secureuri: false,
- fileElementId: 'fileToUpload',
- dataType: 'json',
- data: null,
- success: function (data, status) {
- alert(data);
- },
- error: function (data, status, e) {
- alert(e);
- }
- }
- );
- });
- });
- </script>
- <div>
- @*@Html.BeginForm("Index", "Test", FormMethod.Post, new { @enctype = "multipart/form-data" })
- {*@
- <div>
- <input id="fileToUpload" type="file" size="45" name="fileToUpload" class="input">
- </div>
- <div>
- <button class="button" id="buttonUpload">Upload</button>
- </div>
- @* }*@
- </div>
- jQuery.extend({
- createUploadIframe: function(id, uri)
- {
- //create frame
- var frameId = 'jUploadFrame' + id;
- var iframeHtml = '<iframe id="' + frameId + '" name="' + frameId + '" style="position:absolute; top:-9999px; left:-9999px"';
- if(window.ActiveXObject)
- {
- if(typeof uri== 'boolean'){
- iframeHtml += ' src="' + 'javascript:false' + '"';
- }
- else if(typeof uri== 'string'){
- iframeHtml += ' src="' + uri + '"';
- }
- }
- iframeHtml += ' />';
- jQuery(iframeHtml).appendTo(document.body);
- return jQuery('#' + frameId).get(0);
- },
- createUploadForm: function(id, fileElementId, data)
- {
- //create form
- var formId = 'jUploadForm' + id;
- var fileId = 'jUploadFile' + id;
- var form = jQuery('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');
- if(data)
- {
- for(var i in data)
- {
- jQuery('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form);
- }
- }
- var oldElement = jQuery('#' + fileElementId);
- var newElement = jQuery(oldElement).clone();
- jQuery(oldElement).attr('id', fileId);
- jQuery(oldElement).before(newElement);
- jQuery(oldElement).appendTo(form);
- //set attributes
- jQuery(form).css('position', 'absolute');
- jQuery(form).css('top', '-1200px');
- jQuery(form).css('left', '-1200px');
- jQuery(form).appendTo('body');
- return form;
- },
- ajaxFileUpload: function(s) {
- // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
- s = jQuery.extend({}, jQuery.ajaxSettings, s);
- var id = new Date().getTime()
- var form = jQuery.createUploadForm(id, s.fileElementId, (typeof(s.data)=='undefined'?false:s.data));
- var io = jQuery.createUploadIframe(id, s.secureuri);
- var frameId = 'jUploadFrame' + id;
- var formId = 'jUploadForm' + id;
- // Watch for a new set of requests
- if ( s.global && ! jQuery.active++ )
- {
- jQuery.event.trigger( "ajaxStart" );
- }
- var requestDone = false;
- // Create the request object
- var xml = {}
- if ( s.global )
- jQuery.event.trigger("ajaxSend", [xml, s]);
- // Wait for a response to come back
- var uploadCallback = function(isTimeout)
- {
- var io = document.getElementById(frameId);
- try
- {
- if(io.contentWindow)
- {
- xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
- xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
- }else if(io.contentDocument)
- {
- xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
- xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
- }
- }catch(e)
- {
- jQuery.handleError(s, xml, null, e);
- }
- if ( xml || isTimeout == "timeout")
- {
- requestDone = true;
- var status;
- try {
- status = isTimeout != "timeout" ? "success" : "error";
- // Make sure that the request was successful or notmodified
- if ( status != "error" )
- {
- // process the data (runs the xml through httpData regardless of callback)
- var data = jQuery.uploadHttpData( xml, s.dataType );
- // If a local callback was specified, fire it and pass it the data
- if ( s.success )
- s.success( data, status );
- // Fire the global callback
- if( s.global )
- jQuery.event.trigger( "ajaxSuccess", [xml, s] );
- } else
- jQuery.handleError(s, xml, status);
- } catch(e)
- {
- status = "error";
- jQuery.handleError(s, xml, status, e);
- }
- // The request was completed
- if( s.global )
- jQuery.event.trigger( "ajaxComplete", [xml, s] );
- // Handle the global AJAX counter
- if ( s.global && ! --jQuery.active )
- jQuery.event.trigger( "ajaxStop" );
- // Process result
- if ( s.complete )
- s.complete(xml, status);
- jQuery(io).unbind()
- setTimeout(function()
- { try
- {
- jQuery(io).remove();
- jQuery(form).remove();
- } catch(e)
- {
- jQuery.handleError(s, xml, null, e);
- }
- }, 100)
- xml = null
- }
- }
- // Timeout checker
- if ( s.timeout > 0 )
- {
- setTimeout(function(){
- // Check to see if the request is still happening
- if( !requestDone ) uploadCallback( "timeout" );
- }, s.timeout);
- }
- try
- {
- var form = jQuery('#' + formId);
- jQuery(form).attr('action', s.url);
- jQuery(form).attr('method', 'POST');
- jQuery(form).attr('target', frameId);
- if(form.encoding)
- {
- jQuery(form).attr('encoding', 'multipart/form-data');
- }
- else
- {
- jQuery(form).attr('enctype', 'multipart/form-data');
- }
- jQuery(form).submit();
- } catch(e)
- {
- jQuery.handleError(s, xml, null, e);
- }
- jQuery('#' + frameId).load(uploadCallback );
- return {abort: function () {}};
- },
- uploadHttpData: function( r, type ) {
- var data = !type;
- data = type == "xml" || data ? r.responseXML : r.responseText;
- // If the type is "script", eval it in global context
- if ( type == "script" )
- jQuery.globalEval( data );
- // Get the JavaScript object, if JSON is used.
- if ( type == "json" )
- eval( "data = " + data );
- // evaluate scripts within html
- if ( type == "html" )
- jQuery("<div>").html(data).evalScripts();
- return data;
- }
- })
C#_ajax fileupload的更多相关文章
- .JavaWeb文件上传和FileUpload组件使用
.JavaWeb文件上传 1.自定义上传 文件上传时的表单设计要符合文件提交的方式: 1.提交方式:post 2.表单中有文件上传的表单项:<input type="file" ...
- C#-WebForm-文件上传-FileUpload控件
FileUpload - 选择文件,不能执行上传功能,通过点击按钮实现上传 默认选择类型为所有类型 //<上传>按钮 void Button1_Click(object sender, E ...
- FileUpload组件
package com.itheima.servlet; import java.io.File; import java.io.FileOutputStream; import java.io.IO ...
- 如何实现修改FileUpload样式
这里先隐藏FileUpload 然后用一个input button和一个text来模拟FileUpload 具体代码为 <asp:FileUpload ID="FileUpload1& ...
- 上传文件fileupload
文件上传: 需要使用控件-fileupload 1.如何判断是否选中文件? FileUpload.FileName - 选中文件的文件名,如果长度不大于0,那么说明没选中任何文件 js - f.va ...
- fileupload图片预览功能
FileUpload上传图片前首先预览一下 看看效果: 在专案中,创建aspx页面,拉上FileUpload控件一个Image,将用来预览上传时的图片. <%@ Page Language=&q ...
- textbox button 模拟fileupload
方案一: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.asp ...
- FileUpload 上传文件,并实现c#使用Renci.SshNet.dll实现SFTP文件传输
fileupload上传文件和jquery的uplodify控件使用方法类似,对服务器控件不是很熟悉,记录一下. 主要是记录新接触的sftp文件上传.服务器环境下使用freesshd搭建好环境后,wi ...
- C# 自定义FileUpload控件
摘要:ASP.NET自带的FileUpload控件会随着浏览器的不同,显示的样式也会发生改变,很不美观,为了提高用户体验度,所以我们会去自定义FileUpload控件 实现思路:用两个Button和T ...
随机推荐
- C#调用C++导出类(转)
由于使用别人的Dll,导出的是一个实体类,在C#里封送很难,百度下,有个朋友回复一篇英文的,虽然不一定使用,但可以作为一个知识点,现把原文贴下: c#调用C++写的dll导出类,包含继承,重载等详细介 ...
- Android Application Project 工程目录下各个文件的意思
(1) src:源文件,主要是完成java代码的编写 (2) gen:ADT即系统自动生成的JAVA文件(即源代码目录),程序员千万不要去修改 (3) gen->[Package Name]-& ...
- 【Spark学习】Apache Spark配置
Spark版本:1.1.1 本文系从官方文档翻译而来,转载请尊重译者的工作,注明以下链接: http://www.cnblogs.com/zhangningbo/p/4137969.html Spar ...
- TopCoder SRM 605 DIV1
604的题解还没有写出来呢.先上605的. 代码去practice房间找. 说思路. A: 贪心,对于每个类型的正值求和,如果没有正值就取最大值,按着求出的值排序,枚举选多少个类型. B: 很明显是d ...
- 在Windows上,如何卸载RabbitMQ服务
打开运行->CMD->sc delete RabbitMQ 如果报错..... 打开运行->regedit 找到RabbitMQ节点,删掉即可.(右侧看到的都是启动服务时,需要的配置 ...
- 内核参数优化/etc/sysctl.conf
net.nf_conntrack_max = 65536000net.netfilter.nf_conntrack_tcp_timeout_established = 1200net.ipv4.tcp ...
- C#类、静态类、静态变量,初始化执行顺序
执行顺序: 类的静态变量 ↓ 类的静态构造函数 ↓ 类的普通变量 ↓ 基类的静态变量 ↓ 基类的静态构造函数 ↓ 基类的普通变量 ↓ 基类的构造函数 ↓ 类的构造函数
- [iOS UI进阶 - 3.2] 手势识别器UIGestureRecognizer
A.系统提供的手势识别器 1.敲击手势 UITapGestureRecognizer numberOfTapsRequired: 敲击次数 numberOfTouchesRequired: 同时敲 ...
- OAuth2-Server-php
Yii 有很多 extension 可以使用,在查看了 Yii 官网上提供的与 OAuth 相关的扩展后,发现了几个 OAuth2 的客户端扩展,但是并没有找到可以作为 OAuth2 Server 的 ...
- UVaLive 6625 Diagrams & Tableaux (状压DP 或者 DFS暴力)
题意:给一个的格子图,有 n 行单元格,每行有a[i]个格子,要求往格子中填1~m的数字,要求每个数字大于等于左边的数字,大于上边的数字,问有多少种填充方法. 析:感觉像个DP,但是不会啊...就想暴 ...