js

             navigator.camera.getPicture(function(imageURI){
var url=apiUrl+"/upload.aspx";
//alert(imageURI + "--" +url);
uploadFile(imageURI,url,function(path){ alert(path); });
}, function(message){}, { quality: 50, allowEdit: true,
destinationType: Camera.DestinationType.FILE_URI,sourceType: Camera.PictureSourceType.PHOTOLIBRARY});
//-- 去掉sourceType为拍照
// Upload files to server
var uploadFile=function(imageURI,uploadUrl,getPathCallback) {
try{ var options = new FileUploadOptions();
options.fileKey = "file";
if(imageURI.indexOf("content:")>=0){
options.fileName="random.jpg"
}else{
options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
}
options.mimeType = "image/jpeg";
var ft = new FileTransfer();
ft.upload(
imageURI,
encodeURI(uploadUrl),
function(result) {
console.log('Upload success: ' + result.responseCode);
console.log(result.bytesSent + ' bytes sent');
var path= unescape(eval(result.response).result.Path);
getPathCallback(path);
navigator.camera.cleanup(null,null);
},
function(error) {
console.log('Error uploading file ' + path + ': ' + error.code);
navigator.camera.cleanup(null,null);
},
options); }catch(e){}
}

上传后更新图片地址

/* 修改我的资料 */
module.controller('ModifyMemberInfoCtl', function($scope, $http) { $scope.submit=function(){ } $scope.setImg=function(){ navigator.camera.getPicture(function(imageURI){
var url=apiUrl+"/upload.aspx";
//alert(imageURI + "--" +url);
modal.show();
uploadFile(imageURI,url,function(path){
setTimeout( function(){modal.hide();},3000);
$scope.Avatar=imgUrl + path +"?width=280&height=280";
$scope.$apply();
});
}, function(message){}, { quality: 50, allowEdit: true,
destinationType: Camera.DestinationType.FILE_URI,sourceType: Camera.PictureSourceType.PHOTOLIBRARY}); }
});

C#

<%@ Page Language="C#"Inherits="com.jtys114.Sport.AdminWebUI.Core.PageBase" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="com.jtys114.Sport.AdminWebUI.Core" %>
<%@ Import Namespace="com.jtys114.Sport.Util" %>
<%@ Import Namespace="com.jtys114.Sport.EFModel" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="F.Studio.Web.Utility" %>
<%@ Import Namespace="System.IO" %>
<script runat="server"> static readonly string C_FileRoot = "/Files/";
private bool UseEscape = false;//是否对字符进行escape编码
private String CallBack = ""; // protected override void CheckPageAccessAuth()
{ return;
}
private void WriteJson(object v)
{
var o = new { result = v };
Response.Write(string.Format("{0}({1})", CallBack, JSONhelper.ToJson(o, UseEscape)));
}
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/json";
Response.Expires = -1;
try
{
UseEscape = TryParser<bool>(Request["es"], false);
CallBack = Request["callback"]; if (Request.Files.Count <= 0) throw new Exception("没有文件上传!"); var response= SaveFile(); WriteJson(response); }
catch (Exception ex)
{
WriteJson(new { Msg = ex.Message, Code = -1 });
}
Response.End(); } private UploadResponse SaveFile()
{ var file = Request.Files[0];
var ext = Path.GetExtension(file.FileName); //确保目录存在
string path = C_FileRoot + DateTime.Now.ToString("yyyy-MM-dd") + "/"; if (!Directory.Exists(System.Web.Hosting.HostingEnvironment.MapPath(path)))
{
Directory.CreateDirectory(System.Web.Hosting.HostingEnvironment.MapPath(path));
}
//合成文件名
var filename = path + Guid.NewGuid().ToString("N").Substring(0, 8) + ext; var resp = new UploadResponse();
resp.MIME = file.ContentType;
resp.Size = file.ContentLength / 1024; resp.Name = StringHelper.Escape(Path.GetFileNameWithoutExtension(file.FileName));
resp.Path = StringHelper.Escape(filename);
resp.Code = 200;
resp.Msg = "Success"; using (var ctx = DBCtx.GetCtx())
{
var ent = new Sys_Files();
ent.AddTime = DateTime.Now;
ent.CatalogId = Util.GetQ<int>(Request, "dirNo", -1);
ent.FileSize = resp.Size;
ent.IsDeleted = false;
ent.Name = resp.Name;
ent.Path = resp.Path; ctx.Sys_Files.AddObject(ent);
ctx.SaveChanges(); resp.FileId = ent.DocId;
} //保持文件
file.SaveAs(System.Web.Hosting.HostingEnvironment.MapPath(filename)); return resp;
}

cordova 选择图片并上传到服务器的更多相关文章

  1. HTML5裁剪图片并上传至服务器实现原理讲解

    HTML5裁剪图片并上传至服务器实现原理讲解   经常做项目需要本地上传图片裁剪并上传服务器,比如会议头像等功能,但以前实现这类需求都很复杂,往往需要先把图片上传到服务器,然后返回给用户,让用户确定裁 ...

  2. HTML5 Canvas前台压缩图片并上传到服务器

    1.前台代码: <input id="fileOne" type="file" /> <input id="btnOne" ...

  3. 微信js-sdk,选择图片,上传,下载到本地,php服务端

    //前端js代码<script> //客户端6.0.2 wx.config({ //debug:true, appId: "{pigcms:$signPackage.appId} ...

  4. HTML5 本地裁剪图片并上传至服务器(转)

    很多情况下用户上传的图片都需要经过裁剪,比如头像啊什么的.但以前实现这类需求都很复杂,往往需要先把图片上传到服务器,然后返回给用户,让用户确定裁剪坐标,发送给服务器,服务器裁剪完再返回给用户,来回需要 ...

  5. js实现图片粘贴上传到服务器并展示

    最近看了一些有关于js实现图片粘贴上传的demo,实现如下: (这里只能检测到截图粘贴和图片右键复制之后粘贴) demo1: document.addEventListener('paste', fu ...

  6. SpringMvc + Jsp+ 富文本 kindeditor 进行 图片ftp上传nginx服务器 实现

    一:html 原生态的附件上传 二:实现逻辑分析: 1.1.1 需求分析 Common.js 1.绑定事件 2.初始化参数 3.上传图片的url: /pic/upload 4.上图片参数名称: upl ...

  7. 转:【微信小程序】 微信小程序-拍照或选择图片并上传文件

    调用拍照API:https://mp.weixin.qq.com/debug/wxadoc/dev/api/media-picture.html?t=20161222#wxchooseimageobj ...

  8. 小程序踩坑记录-上传图片及canvas裁剪图片后上传至服务器

    最近在写微信小程序的上传图片功能,趟过了一些坑记录一下. 想要满足的需求是,从手机端上传图片至服务器,为了避免图片过大影响传输效率,需要把图片裁剪至适当大小后再传输 主要思路是,通过wx.choose ...

  9. 微信小程序压缩图片并上传到服务器(拿去即用)

    这里注意一下,图片压缩后的宽度是画布宽度的一半 canvasToTempFilePath 创建画布的时候会有一定的时间延迟容易失败,这里加setTimeout来缓冲一下 这是单张图片压缩,多张的压缩暂 ...

随机推荐

  1. FireFox 火狐主页被劫持

    火狐主页被劫持hao123,流氓 WIN7 ,firefox,任务栏,快速启动,右键 属性 target 应该是 "D:\Program Files (x86)\Mozilla Firefo ...

  2. springMVC学习记录3-拦截器和文件上传

    拦截器和文件上传算是springmvc中比较高级一点的内容了吧,让我们一起看一下. 下面先说说拦截器.拦截器和过滤器有点像,都可以在请求被处理之前和请求被处理之到做一些额外的操作. 1. 实现Hand ...

  3. es进行curl请求时报错:missing authentication token for REST request

    把host 修改一下即可 ELASTICSEARCH_HOST=http://user:password@URL:port

  4. pyplot图像组件

    pyplot图像组件 ax子对象的组件内容 Title 图表标题 plt.title() Axis 坐标范围,x轴,y轴 plt.axis() label 坐标轴标注 plt.xlabel() plt ...

  5. tflearn 实现DNN 全连接

    https://github.com/tflearn/tflearn/blob/master/examples/others/recommender_wide_and_deep.py import n ...

  6. Concurrency and Race Conditions

    1.当多个线程访问共享硬件或软件资源的任何时候,由于线程之间可能产生对资源的不一致观察,所以必须显式管理对资源的访问. 2.内核中的并发管理设施: (1). 信号量: P操作将信号量的值减 1 ,判断 ...

  7. php 过滤emoji

    function filter_emoji_string($str){ $str = trim($str); $str = preg_replace_callback('/./u',function ...

  8. redis的五种常见数据类型的常用指令

      一.String字符串,key-value 应用场景:string是redis的最基本数据类型,key-value格式,一个key对应一个值的情况下 1.设置key = value:set key ...

  9. padding-top和margin-top的区别

    学习前端知识,对我们查找页面元素很有帮助,而且自己在原公司时,有参与一个QA系统,自己去设计了这个产品,出了原型图,同时设计了几个页面,希望通过做这个产品提高自己的技术,可是因为离职,所以计划搁浅了, ...

  10. 解决selenium不支持firefox低版本的问题

    解决selenium不支持firefox低版本的问题 在火狐浏览器升级后,突然发现webdriver运行脚本的时候不能调出火狐浏览器了,并报错WebDriverException:Message:'C ...