azure - Azure Web App Bot - 访问本地资源
问题描述
我的 Web App Bot 应该根据请求返回图像。图像位于文件夹中的 .csproj 中,具有以下配置
将图像发送给用户的源代码
var imgMessage = context.MakeMessage();
var attachment = new Attachment();
attachment.ContentUrl = $"{HttpContext.Current.Request.Url.Scheme}://{HttpContext.Current.Request.Url.Authority}/Resources/{InvocationName}/{InvocationNameExtension}.jpg";
attachment.ContentType = "image/jpg";
attachment.Name = "Image";
context.PostAsync(attachment.ContentUrl);
虽然它在本地工作,但在发布到 Azure 云后就无法工作。但是,Azure 云的路径类似于:h ttps://xxxx.azurewebsites.net/Resources/img/Cafeteria.jpg
FTP 上传确实包含该文件
2>Adding file (xxxx\bin\Resources\img\Cafeteria.jpg).
该文件在服务器上,但无法访问。我应该如何包含位于 .csproj 中的图像?由于独立性,我不想引用外部 URL。
解决方案
将构建操作更改为:“嵌入式资源”。
string resourceFile = ResourceManager.FindResource(InvocationName, InvocationNameExtension);
string resourceFileExtension = ResourceManager.GetResourceExtension(resourceFile);
var attachment = new Attachment();
attachment.ContentUrl = BuildImageUrl(resourceFile, resourceFileExtension);
attachment.ContentType = $"image/{resourceFileExtension}";
private string ConvertToBase64(string resourceFile) => Convert.ToBase64String(ResourceManager.GetBytes(resourceFile));
private string BuildImageUrl(string resourceFile, string resourceFileExtension) => "data:image/" + resourceFileExtension + ";base64," + ConvertToBase64(resourceFile);
使用这种方法,我通过 base64 直接将图像的内容发送给用户。奇迹般有效
推荐阅读
- python - 如何在我的网站上管理奇怪的字体?
- c# - 无法执行 Cookie 身份验证:SignInAsync 和 AuthenticateAsync 不成功
- php - 如何根据索引php合并两个数组?
- javascript - 为什么将异步等待放在数组映射回调中?
- postgresql - 使用 postgresql 和 typeorm 命令获取 100km 内的实体
- loops - 如何根据数据框日期范围自动调整带有 seaborn 的 xlim
- php - CakePHP 3:保存 hasOne 关联($_accessible 未设置)
- model - 如何使用 VGG19 迁移学习预训练
- python - 合并其他列匹配的数据框的值
- python - 为什么 pandas read_csv 能够找到文件但 os.listdir 不能?