zoukankan      html  css  js  c++  java
  • cordova 选择图片并上传到服务器

    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为拍照
    View Code
    // 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){}
    }
    View Code

    上传后更新图片地址

    /* 修改我的资料 */
    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});
            
        }
    });
    View Code

    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;
        }
    View Code
  • 相关阅读:
    windows环境变量
    软件工程的一般过程和需要的文档
    linux find 命令查找文件和文件夹
    mybatis中mapUnderscoreToCamelCase自动驼峰命名转换
    人体湿气重有哪些表现? 细数湿气重的危害
    MySQL升级后1728错误解决方案
    linux清理Java环境
    无线投屏PC投电视
    report studio 交叉表占比
    Cognos审核模块的导入与设置
  • 原文地址:https://www.cnblogs.com/wdfrog/p/4642645.html
Copyright © 2011-2022 走看看