前端:

 <!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跨域上传文件的更多相关文章

  1. Angular4 后台管理系统搭建(10) - 做一个通用的可跨域上传文件的组件

    写的很慢,不知不觉这是第十篇了.但是我其他事情太多,只能抽空写下.现在angular4或angular2流行的上传方式是ng2-file-upload.它的功能很强大.但是我没有配置成可以跨域上传的. ...

  2. js跨域上传文件 iframe

    封装好的jq插件 (function () { var iframe = '<iframe name="jqUploadIframe" style="display ...

  3. iframe无刷新跨域上传文件并获取返回值

    通常我们会有一个统一的上传接口,这个接口会被其他的服务调用.如果出现不同域,还需要无刷新上传文件,并且获取返回值,这就有点麻烦了.比如,新浪微博启用了新域名www.weibo.com,但接口还是使用原 ...

  4. ASP.NET跨服务器上传文件的相关解决方案

    第一种:通过FTP来上传文件 首先,在另外一台服务器上设置好FTP服务,并创建好允许上传的用户和密码,然后,在ASP.NET里就可以直接将文件上传到这台 FTP 服务器上了.代码如下: <%@ ...

  5. iframe无刷新跨域上传文件并获得返回值

    原文:http://geeksun.iteye.com/blog/1070607 需求:从S平台上传文件到R平台,上传成功后R平台返回给S平台一个值,S平台是在一个页面弹出的浮窗里上传文件,所以不能用 ...

  6. springmvc结合ajax实现跨域上传文件

    本方法的思路是:先在前端利用FileReader将图片转换成base64编码,然后将编码字符串形式传递到后台(前提是服务端设置了允许跨域),后端再把base64编码转换成图片. 前端代码: <! ...

  7. [javascript] ajaxfileupload.js 跨域上传文件

    原文地址: http://www.ueffort.com/jqueryajaxfileupload-js-duo-wen-jian-shang-chuan-chuan-zhi-kua-yu/ 跨域 这 ...

  8. POST方式跨域上传文件

    JSONP请求有限制: 第一,不能跳出两层, 第二,不支持POST. 往往解决跨域POST请求的方案是个"古老"方法, 请求同域下的iframe. 服务器端:  需要附加头信息: ...

  9. springmvc跨域上传文件问题

    把以下文件放到webapps的root文件夹下: 1.clientaccesspolicy.xml <?xml version="1.0" encoding="ut ...

随机推荐

  1. 获取MP3和M4A音乐文件的歌曲信息以及专辑图片--备用

    NSBundle* bundle = [NSBundle mainBundle];     NSString* path = [bundle bundlePath];     NSURL * file ...

  2. Android 之Service

    service是运行在后台的服务,你可以启动一个服务Service来播放音乐,或者记录你地理信息位置的改变,或者启动一个服务来运行并一直监听某种动作. 接下来分析一下service 的生命周期: 1: ...

  3. 一句话输出NGINX日志访问IP前十位排行

    AWK的数组字段自增加,然后取值的方法,要记得哟. 还有,SORT指定列排行,也常用的.

  4. ubuntu安装使用kdevelop

    vim是好用,但是有时候操作不那么人性化,用一个ide可能感觉比较好,ubuntu下面比较好的c/c++ ide出名的有kdevelop,Qt等. kdevelop安装: sudo apt-get u ...

  5. ffdshow 源代码分析1 : 整体结构

    ffdshow是一个非常强大的DirectShow解码器,封装了ffmpeg,libmpeg2等解码库.它也提供了丰富的加工处理选项,可以锐化画面,调节画面的亮度等等.不止是视频,FFDShow现在同 ...

  6. unix网络编程之简介

    通常客户一次只与一个服务器通信, 上图为:一个服务器同时处理多个客户的请求. 上图为:客户与服务器使用TCP协议在同一个以太网中通信. 路由器是广域网的架构设备.今天,最大的广域网是因特网. 上图为: ...

  7. HDOJ 1196 Lowest Bit(二进制相关的简单题)

    Problem Description Given an positive integer A (1 <= A <= 100), output the lowest bit of A. F ...

  8. openstack debugs

  9. <WinForm_1>初识WinForm编程

    暑假花了一个多月自学了Win32 SDK编程,不敢说成为了高手,但是还是颇具收获——windows的消息机制(著名的消息循环).一切控件均是窗口…… 曾经有一位网友(和我一个专业的)向我吐槽:Win3 ...

  10. lesson7:java线程池

    在jdk1.5的版本中,java提供了语言级别的线程池,对于需要使用线程池的业务系统和中间件框架等提供了方便的选择.我认为线程池主要有两个方面的作用:第一就是防止因为外部条件的变化,造成线程数的瞬间飙 ...