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         }

 

posted @ 2019-11-06 11:23  w1433189426  阅读(695)  评论(0编辑  收藏  举报