asp.net跨域上传文件
前端:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<input type="file" id="myphoto" />
<div style="display:none" id="div_div1">
<img src="#" id="img_myphoto" />
</div>
</body>
</html>
<script src="assets/js/jquery-1.7.1.js"></script>
<script type="text/javascript">
$("#myphoto").change(function () {
var file = this.files[0];
var formData = new FormData();
formData.append('tFile', file);
jQuery.support.cors = true;
$.ajax({
type: "POST", // 必须用post
url: "http://118.200.100.100:8080/uploadStudentIDCard.ashx?studentID=100",
crossDomain: true,
data: formData,
contentType: false,
processData: false,
success: function (data) {
if (data.result == 1) {
$("#img_myphoto").attr("src", "http://114.215.198.241:8086" + data.url);
$("#div_div1").show();
}
},
error: function (res) {
alert('shit');
}
});
});
</script>
后端:
using JSXiangYu.JiSuJob.BLLFactory;
using JSXiangYu.JiSuJob.IBLL;
using JSXiangYu.JiSuJob.Utility;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web; namespace JSXiangYu.JiSuJob.WebApp.student.person
{
public class ResultData
{
public int _result;
public string _msg;
}
/// <summary>
/// uploadStudentIDCard 的摘要说明
/// </summary>
public class uploadStudentIDCard : IHttpHandler
{
IBLLSession bllSession = BLLSessionFactory.CreateBLLSession();//获取业务逻辑会话对象
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "Application/json"; UploadStudentIDCard(context);
} /// <summary>
/// 上传学生证
/// </summary>
/// <param name="context"></param>
private void UploadStudentIDCard(HttpContext context)
{
context.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, Accept,X-Requested-With");
HttpContext.Current.Response.End();
} //1.采集数据
//string kk = context.Request.Params["jsoncallback"];
string studentIDStr = context.Request.Params["studentID"];//学生编号
HttpPostedFile file = context.Request.Files["tFile"];//学生证
//2.数据校验
int studentID;
if (!int.TryParse(studentIDStr, out studentID))
{
context.Response.Write("{\"result\":2}");//学生编号格式有误(学生未登录)
context.Response.End();
return;
}
if (file == null)
{
context.Response.Write("{\"result\":3}");//未上传学生证
context.Response.End();
return;
} //3.处理业务逻辑
//3.1处理存储文件的路径
string fileName = Path.GetFileName(file.FileName);//文件的全名称
string ext = fileName.Substring(fileName.LastIndexOf('.'));//文件的扩展名
string dateBasePath;//保存身份证的相对路径
string dateBasePath_m;//手机展示相对路径
string path_m;//手机展示物理路径
string guidStr = Guid.NewGuid().ToString() + ext;//图片名称用Guid命名
//3.2物理路径
string path = GetUploadFilePath(out dateBasePath, out dateBasePath_m, out path_m) + guidStr;//物理路径+文件全名称
path_m += guidStr;//物理路径——手机端
//3.3相对路径
dateBasePath += guidStr;//相对路径+文件全名称
dateBasePath_m += guidStr;//相对路径——手机端
//3.4处理图片+上传图片
Bitmap bmBig = new Bitmap(file.InputStream);
FileExt.MakeThumbnail(bmBig, path, , );
Bitmap bmBig2 = new Bitmap(file.InputStream);
FileExt.MakeThumbnail(bmBig2, path_m, , );
//3.5判断学生账号是否存在
IStudentServer _studentServer = bllSession.StudentServer;
if (!_studentServer.IsExistStudentAccount(studentID))
{
context.Response.Write("{\"result\":4}");//不存在该学生账号
context.Response.End();
return;
}
//3.6更新学生信息
if (_studentServer.UpdateStudentInfoOfStudentIDCard(studentID, dateBasePath, dateBasePath_m))
{
context.Response.Write("{\"result\":1,\"url\":\"" + dateBasePath_m + "\"}");//图片上传成功
context.Response.End();
return;
}
else
{
context.Response.Write("{\"result\":0}");//图片上传失败
context.Response.End();
return;
}
} /// <summary>
/// 处理保存学生证的路径
/// </summary>
/// <param name="newPath">相对路径</param>
/// <returns>返回物理路径</returns>
private string GetUploadFilePath(out string newPath, out string dateBasePath_m, out string path_m)
{
DateTime date = DateTime.Now;
string tempPath = date.Year + "/" + date.Month + "/" + date.Day + "/"; string oldPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "assets/img/student/studentIDCard/");//原始路径
oldPath += tempPath;//压缩图片物理路径 path_m = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "assets/img/student/studentIDCard_m/");//原始路径
path_m += tempPath;//压缩图片物理路径——手机端 if (!Directory.Exists(oldPath))
{
Directory.CreateDirectory(oldPath);
}
if (!Directory.Exists(path_m))
{
Directory.CreateDirectory(path_m);
}
newPath = "/assets/img/student/studentIDCard/" + tempPath;//压缩图片相对路径
dateBasePath_m = "/assets/img/student/studentIDCard_m/" + tempPath;//压缩图片相对路径——手机端
return oldPath;
} public bool IsReusable
{
get
{
return false;
}
}
}
}
asp.net跨域上传文件的更多相关文章
- Angular4 后台管理系统搭建(10) - 做一个通用的可跨域上传文件的组件
写的很慢,不知不觉这是第十篇了.但是我其他事情太多,只能抽空写下.现在angular4或angular2流行的上传方式是ng2-file-upload.它的功能很强大.但是我没有配置成可以跨域上传的. ...
- js跨域上传文件 iframe
封装好的jq插件 (function () { var iframe = '<iframe name="jqUploadIframe" style="display ...
- iframe无刷新跨域上传文件并获取返回值
通常我们会有一个统一的上传接口,这个接口会被其他的服务调用.如果出现不同域,还需要无刷新上传文件,并且获取返回值,这就有点麻烦了.比如,新浪微博启用了新域名www.weibo.com,但接口还是使用原 ...
- ASP.NET跨服务器上传文件的相关解决方案
第一种:通过FTP来上传文件 首先,在另外一台服务器上设置好FTP服务,并创建好允许上传的用户和密码,然后,在ASP.NET里就可以直接将文件上传到这台 FTP 服务器上了.代码如下: <%@ ...
- iframe无刷新跨域上传文件并获得返回值
原文:http://geeksun.iteye.com/blog/1070607 需求:从S平台上传文件到R平台,上传成功后R平台返回给S平台一个值,S平台是在一个页面弹出的浮窗里上传文件,所以不能用 ...
- springmvc结合ajax实现跨域上传文件
本方法的思路是:先在前端利用FileReader将图片转换成base64编码,然后将编码字符串形式传递到后台(前提是服务端设置了允许跨域),后端再把base64编码转换成图片. 前端代码: <! ...
- [javascript] ajaxfileupload.js 跨域上传文件
原文地址: http://www.ueffort.com/jqueryajaxfileupload-js-duo-wen-jian-shang-chuan-chuan-zhi-kua-yu/ 跨域 这 ...
- POST方式跨域上传文件
JSONP请求有限制: 第一,不能跳出两层, 第二,不支持POST. 往往解决跨域POST请求的方案是个"古老"方法, 请求同域下的iframe. 服务器端: 需要附加头信息: ...
- springmvc跨域上传文件问题
把以下文件放到webapps的root文件夹下: 1.clientaccesspolicy.xml <?xml version="1.0" encoding="ut ...
随机推荐
- office2003万能密钥
保证有效 OFFICE 2003 : GWH28-DGCMP-P6RC4-6J4MT-3HFDY
- PHP 中的静态变量的简单使用
静态变量的初始化只能在第一次static 声明的时候进行,这些静态变量只能在声明他的函数中访问到. 例如: <?php function do_something(){ static $firs ...
- uva 10036 Problem C: Divisibility
题意:能否在一个整数序列的每相邻的两项之间添加一个加减号,使得最终结果能被一个给定整数K<=100整除. dp[i][j]表示第i个数取余k为j的布尔值. #include <cstdio ...
- rsync同步目录及同步文件
最简单的只读同步工作. 一,服务端的配置 1,安装rsync(阿里云默认已有此程序) 略 2,生成文件rsyncd.conf,内容如下: #secrets file = /etc/rsyncd.sec ...
- C51程序优化
1.指针: 对于大部分的编译器,使用指针比使用数组生成的代码更短,执行效率更高.但是在Keil中则相反,使用数组比使用的指针生成的代码更短.通常使用自加.自减指令和复合赋值表达式(如a-=1及a+=1 ...
- CH Round #54 - Streaming #5 (NOIP模拟赛Day1)
A.珠 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2354%20-%20Streaming%20%235%20(NOIP模拟赛Day1)/珠 题解:sb题, ...
- 【转】使用dos2unix批量转换文件
原文网址:http://kuaile.in/archives/1208 dos2unix是Linux下的一个用户转换格式的程序,由于windows上文件的结束符和linux上的不同,那么在window ...
- js原型继承深入
js采用原型继承来实现类的派生,但是原型链再深入点,我们又知道多少呢,现在不妨往下看: 先来一个原型继承: var M1 = function() { this.param = "m1's ...
- c语言学习笔记(1)——c语言的特点
一.c语言概述 1.为什么学习c语言? (1)c的起源和发展 第一代语言:机器语言 1101 第二代语言:汇编语言 add ax,bx; 第三代语言:高级语言 a+b; 结构化:c fortra ...
- Beanstalkd(ubuntu安装)
安装Beanstalkd # apt-get install beanstalkd Unubtu 开启beanstalkd的持久化选项 #vim /etc/default/beanstalkd 把S ...