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 ...
随机推荐
- 【行为型】Interpreter模式
解释器模式意图为给定的语言定义其文法表示,同时定义该文法表示的一套解释器来解释语言中的句子.该模式说的简单通俗点,其主要用途是用来解释用的.至于解释什么,则要看具体的上下文环境.我们可以为一个表达式专 ...
- == 与 is
Python中的对象包含三要素:id.type.valueid:用来唯一标识一个对象: type:标识对象的类型: value是对象的值. ==是判断对象的值是否相等,也就是value,is是判断对象 ...
- [Python 3.x 官方文档翻译]The Python Tutorial Python教程
Python is an easy to learn, powerful programming language. It has efficient high-level data structur ...
- Properties配置文件
package file; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; ...
- ES6-2
向ES6看齐,用更好的JavaScript(二) 上一篇 中介绍了关于变量部分的新特性,本篇将从现有对象的拓展来展开介绍 1 增加了模板字符串 先看一下,ES6之前我们是如何实现输出模板的: do ...
- CSS浮动属性Float详解
什么是CSS Float? float 是 css 的定位属性.在传统的印刷布局中,文本可以按照需要围绕图片.一般把这种方式称为“文本环绕”.在网页设计中,应用了CSS的float属性的页面元素就像在 ...
- 在Activity的生命周期中,会被系统回调的方法
onCreate(Bundle savedStatus):创建Activity时被回调.onStart():启动Activity时被回调.onRestart():重新启动Activity时被回调.on ...
- pydev package包中__init__.py作用
Eclipse用pydev,新建一个pydev package时,总会自动地生成一个空的__init__.py文件. 原来在python模块的每一个包中,都有一个__init__.py文件(这个文件定 ...
- 8.2.1.8 IS NULL Optimization NULL 优化:
8.2.1.8 IS NULL Optimization NULL 优化: Oracle 对待null值: SQL> create table t100(id int,name char(10) ...
- N次剩余
$$求解x^n\equiv a(\%P),其中P是质数,0\leq x<P$$ 设$g$是$P$的原根 那么$g^0,g^1,...,g^{P-2}$和$1,2,...,P-1$是一一对应的. ...