• HttpContext.Current.Server.MapPath("") 未将对象设置到引用的


    在多线程中使用该方法获取目录报错:未将对象设置到引用

     1 #region 上传图片到腾讯
     2         public async Task<Result> UploadImageToWX(string background_pic_url)
     3         {
     4             var _result = new Result();
     5             var token = GetWxGhAuthorizerAccessToken(AbpSession.ShopId);
     6             if (!token.success)
     7             {
     8                 _result.Code = 0;
     9                 _result.Message = token.message;
    10                 return _result;
    11             }
    12 
    13             try
    14             {
    15                 var filename = background_pic_url.Substring(background_pic_url.LastIndexOf('/'));
    16                 if (filename.LastIndexOf('.') < 0)
    17                 {
    18                     _result.Code = 0;
    19                     _result.Message = "图片错误";
    20                     return _result;
    21                 }
    22                 var suffix = filename.Substring(filename.LastIndexOf('.'));
    23 
    24                 //报错代码
    25                 //string path = System.Web.HttpContext.Current.Server.MapPath("/upload/images/" + DateTime.Now.ToString("yyyyMM"));
    26 
    27                 //解决方案
    28                 string path = MapPath("/upload/images/" + DateTime.Now.ToString("yyyyMM"));
    29 
    30                 if (!Directory.Exists(path))
    31                 {
    32                     Directory.CreateDirectory(path);
    33                 }
    34                 string file = path + "/" + Guid.NewGuid().ToString("n") + suffix;
    35 
    36                 Uri uri = new Uri(background_pic_url);
    37                 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
    38                 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    39                 Stream resStream = response.GetResponseStream();//得到图片数据流
    40                 Bitmap sourcebm = new Bitmap(resStream);//初始化Bitmap图片
    41                 sourcebm.Save(file);
    42 
    43                 var tokenResult = token.data as token_model;
    44 
    45                 var uploadResult = await Senparc.Weixin.MP.AdvancedAPIs.MediaApi.UploadImgAsync(tokenResult.token, file);
    46                 _result.Code = 1;
    47                 _result.Data = uploadResult;
    48             }
    49             catch (Exception ex)
    50             {
    51 
    52                 _result.Message = ex.Message;
    53 
    54                 _result.Code = 0;
    55 
    56             }
    57 
    58 
    59             return _result;
    60         }
    61         #endregion

    解决方法:

     1     /// <summary>
     2         /// 获取文件路径
     3         /// </summary>
     4         /// <param name="strPath"></param>
     5         /// <returns></returns>
     6         private string MapPath(string strPath)
     7         {
     8             if (HttpContext.Current != null)
     9             {
    10                 return HttpContext.Current.Server.MapPath(strPath);
    11             }
    12             else //非web程序引用             
    13             {
    14                 strPath = strPath.Replace("/", "\");
    15                 if (strPath.StartsWith("\"))
    16                 {
    17                     strPath = strPath.TrimStart('\');
    18                 }
    19                 return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
    20             }
    21         }
  • 相关阅读:
    asp.net 通过js调用webService注意
    身份证号码验证 类
    char值码对应大全
    C# 让textbox 只能输入数字的方法
    table嵌套循环数据
    拆分字符串
    angular的路由配置
    js对象数组(JSON) 根据某个共同字段 分组
    当前时间的后七天
    ajax提交时“加载中”提示的处理方法
  • 原文地址:https://www.cnblogs.com/WQ1992/p/11804047.html
Copyright © 2020-2023  润新知