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. IE浏览器中的加载项怎么删除

    IE浏览器中的加载项是一些软件或者浏览器的功能控件,我们可以通过禁用.开启来控制是否使用某些加载项,同时可以将一些加载项删除. 比如当我们遇到了一些不好的加载项,想要将它删除,通过这篇经验,教大家怎么 ...

  2. Redis基本操作-string

    Redis的5种数据结构:string.list.hash.set和zset; Redis 所有的数据结构都是以唯一的 key 字符串作为名称,然后通过这个唯一 key 值来获取相应的 value 数 ...

  3. mysql insert on duplicate key, update, ignore

    insert 语句中不能使用where,所以如果需要根据插入的数据在已有的数据库表是否重复做一些操作可以使用下面三种方法: 1. 使用insert,捕获duplicate错误 2. insert in ...

  4. 转载:MySQL和Redis 数据同步解决方案整理

    from: http://blog.csdn.net/langzi7758521/article/details/52611910 最近在做一个Redis箱格信息数据同步到数据库Mysql的功能. 自 ...

  5. 编写jQuery插件(一)——插件约定及插件中的闭包

    编写插件的目的是给已经有的一系列方法或函数做一个封装,以便在其他地方重复使用,提高开发效率和方便后期维护. 在编写jQuery插件的时候,我们一般会遵循一些约定: jQuery插件推荐命名为:jque ...

  6. TEXT 3 Food firms and fat-fighters

    TEXT 3 Food firms and fat-fighters 食品公司与减肥斗士 Feb 9th 2006 From The Economist Global Agenda Five lead ...

  7. Haskell语言学习笔记(61)Distributive

    Distributive class Functor g => Distributive g where distribute :: Functor f => f (g a) -> ...

  8. 迷你MVVM框架 avalonjs 1.3.8发布

    avalon1.3.8主要是在ms-repeat. ms-each. ms-with等循环绑定上做重大性能优化,其次是对一些绑定了事件的指令添加了roolback,让其CG回收更顺畅. 重构ms-re ...

  9. MS-SQL

    变量 一个@为局部变量,两个@@为全局变量 @@error 最后一句SQL语句的错误编号 错误码 @@identity最后一次插入的标示值符 insert into biao(lie) output ...

  10. Tensorflow从文件读取数据

    http://blog.csdn.net/zengxyuyu/article/details/53289906