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

 #region 上传图片到腾讯
public async Task<Result> UploadImageToWX(string background_pic_url)
{
var _result = new Result();
var token = GetWxGhAuthorizerAccessToken(AbpSession.ShopId);
if (!token.success)
{
_result.Code = ;
_result.Message = token.message;
return _result;
} try
{
var filename = background_pic_url.Substring(background_pic_url.LastIndexOf('/'));
if (filename.LastIndexOf('.') < )
{
_result.Code = ;
_result.Message = "图片错误";
return _result;
}
var suffix = filename.Substring(filename.LastIndexOf('.')); //报错代码
//string path = System.Web.HttpContext.Current.Server.MapPath("/upload/images/" + DateTime.Now.ToString("yyyyMM")); //解决方案
string path = MapPath("/upload/images/" + DateTime.Now.ToString("yyyyMM")); if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string file = path + "/" + Guid.NewGuid().ToString("n") + suffix; Uri uri = new Uri(background_pic_url);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();//得到图片数据流
Bitmap sourcebm = new Bitmap(resStream);//初始化Bitmap图片
sourcebm.Save(file); var tokenResult = token.data as token_model; var uploadResult = await Senparc.Weixin.MP.AdvancedAPIs.MediaApi.UploadImgAsync(tokenResult.token, file);
_result.Code = ;
_result.Data = uploadResult;
}
catch (Exception ex)
{ _result.Message = ex.Message; _result.Code = ; } return _result;
}
#endregion

解决方法:

     /// <summary>
/// 获取文件路径
/// </summary>
/// <param name="strPath"></param>
/// <returns></returns>
private string MapPath(string strPath)
{
if (HttpContext.Current != null)
{
return HttpContext.Current.Server.MapPath(strPath);
}
else //非web程序引用             
            {
strPath = strPath.Replace("/", "\\");
if (strPath.StartsWith("\\"))
{
strPath = strPath.TrimStart('\\');
}
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
}

HttpContext.Current.Server.MapPath("") 未将对象设置到引用的的更多相关文章

  1. HttpContext.Current.Server.MapPath("/") 未将对象设置到对象的实例异常。

    多线程中的System.Web.HttpContext.Current.Server.MapPath("/") 多线程中Server.MapPath会失效... 网上找到几种解决方 ...

  2. System.Web.HttpContext.Current.Server.MapPath("~/upload/SH") 未将对象引用设置为实例对象

    做项目的时候,System.Web.HttpContext.Current.Server.MapPath("~/upload/SH")   获取路径本来这个方法用的好好的 因为需要 ...

  3. string s = HttpContext.Current.Server.MapPath("");

    string s = HttpContext.Current.Server.MapPath(""); 获取当前文件夹路径 而后用相对路径读取图片

  4. HttpContext.Current.Session[strName]未将对象引用设置到对象的实例

    项目开发是在4.5.1上,不知道为啥客户提供的服务器上安装的是4.5,差别不大也没去升级,然后部署MVC的时候web.config报错 <system.web> <compilati ...

  5. HttpContext.Current.Server未将对象引用到实例

    问题描述: 在一些类库中需要读取当前系统的xml文件,当时用HttpContext.Current无法找到实例化对象 解决代码如下: XmlDocument xml = new XmlDocument ...

  6. win7下解决vs2015新建项目,提示“未将对象引用设置到引用实例“的问题

    问题描述: 打开vs2015新建c++项目时,出现有如下内容的对话框“未将对象引用设置到引用实例”的提示 解决方法: 1.  温馨提示:千万不要一冲动,就去卸载vs2015!! win7下安装vs20 ...

  7. ASP.NET多线程下使用HttpContext.Current为null解决方案 2015-01-22 15:23 349人阅读 评论(0) 收藏

    问题一:多线程下获取文件绝对路径 当我们使用HttpContext.Current.Server.MapPath(strPath)获取绝对路径时HttpContext.Current为null,解决办 ...

  8. ASP.NET多线程下使用HttpContext.Current为null解决方案 2015-01-22 15:23 350人阅读 评论(0) 收藏

    问题一:多线程下获取文件绝对路径 当我们使用HttpContext.Current.Server.MapPath(strPath)获取绝对路径时HttpContext.Current为null,解决办 ...

  9. 转:Server.MapPath相关

    如果你从Page类继承的类中执行这条语句,才可以简单地使用 DataBase = Server.MapPath("data.mdb");否则写全命名空间:System.Web.Ht ...

随机推荐

  1. OC-类似歌词字体颜色逐字变化的实现方法

    预期实效果图如下: 如上图所示,文字的颜色会根据时间的移动,逐字变成绿色. 实现方法:(1)调用方法: 用 void UIRectFillUsingBlendMode(CGRect rect, CGB ...

  2. 一文解读MPA/SPA(转)

    应用模式 模式示意图 多页面应用 每一次页面跳转的时候,后台服务器都会返回一个新的html文档,这种类型的网站也就是多页网站,也叫多页应用. 页面跳转: 返回HTML优点: 首屏时间快,SEO效果好缺 ...

  3. 微信小程序API交互反馈,wx.showToast显示消息提示框

    导读:wx.showToast(OBJECT) 显示消息提示框. OBJECT参数说明: 参数 类型 必填 说明 最低版本 title String 是 提示的内容 icon String 否 图标, ...

  4. [Go] 使用protobuf进行序列化和反序列化

    先定义消息类型 orders.proto syntax = "proto2"; package message; message Orders { required int32 o ...

  5. May 26th, 2019. Week 22nd, Sunday

    A real loser is somebody that's so afraid of not winning, they don't even try. 真正的失败者,是那些因为害怕不能成功,就连 ...

  6. hibernate关联关系一对多

    1. 什么是关联(association) 1.1 关联指的是类之间的引用关系.如果类A与类B关联,那么被引用的类B将被定义为类A的属性 首先建2个实体类 Order.java package com ...

  7. RabbitMQ学习笔记(四、RabbitMQ队列)

    目录: 消息路由失败了会怎样 备份交换器 TTL与DLX 如何实现延迟队列 RabbitMQ的RPC实现 持久化 事务 发送方确认机制 消息路由失败了会怎样: 在RabbitMQ中,如果消息路由失败了 ...

  8. luoguP3649 [APIO2014]回文串

    题意 关于回文自动机的讲解见这里 由于回文串个数是\(O(n)\)的,直接回文自动机上统计并比较即可. code: #include<bits/stdc++.h> using namesp ...

  9. eclispe git config配置文件配置远程仓库

    git config基本配置如下: [core] repositoryformatversion = 0 filemode = false logallrefupdates = true [remot ...

  10. Go 字节 (byte) & 文字符号 (rune)

    byte 通过 byte 定义一个字节,字节必须使用单引号包起来,直接打印字节输出的是 ascii 码,需要通过格式化输出 byte 是 uint8 的别称,使用 byte 主要是为了区分字节和无符号 ...