遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

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
复制代码

 

posted on   遗忘海岸  阅读(1504)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2013-07-13 RabbitMQ的一些说明
2010-07-13 多分类产品查询
点击右上角即可分享
微信分享提示