c# - 无法从我的网络应用程序中的其他服务器获取图像
问题描述
我正在尝试使用通过网络的 IP 地址从其他服务器检索图像,但无法在我的 Web 应用程序中获取图像。
我尝试了以下方法来编写图像路径但无法获取图像:
C#:
string imagePath = @"http://192.168.10.245/Shared/1.jpg";
或者
string imagePath = @"file://192.168.10.245/Shared/1.jpg";
或者
string imagePath = @"\\192.168.10.245\Shared\1.jpg";
或者
string imagePath = @"192.168.10.245/Shared/1.jpg";
emp_img.ImageUrl = imagePath;
aspx:
<asp:Image runat="server" ID="emp_img" CssClass="imgstyle" />
请注意,图像放置在共享文件夹中,该文件夹在 Windows 资源管理器中可以正常打开
请帮我解决这个问题
我经历过这个
谢谢
更新:
我创建了一个图像处理程序来实现这一点:
ImgHandler.ashx:
public class ImgHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "image/jpg";
string EmpCode = context.Request.Params["EmpCode"].ToString();
string path = "//192.168.10.245\\Shared\\"+EmpCode+".jpg";
context.Response.WriteFile(path);
}
public bool IsReusable {
get {
return false;
}
}
}
aspx:
<img src="ImgHandler.ashx?EmpCode=1" style="max-width:250px; max-height:250px;" />
我仍然无法获取图像,请帮我写路径
解决方案
The following code resolved my Problem:
ashx:
public void ProcessRequest (HttpContext context) {
try
{
context.Response.ContentType = "image/jpg";
string FileName = context.Request.Params["FileName"].ToString();
string path = System.Configuration.ConfigurationManager.AppSettings["ProfilePhotoPath"].ToString()+FileName;
byte[] pic = GetImage(path);
if (pic != null)
{
context.Response.WriteFile(path);
}
}
catch (Exception ex)
{
}
}
private byte[] GetImage(string iconPath)
{
using (WebClient client = new WebClient())
{
byte[] pic = client.DownloadData(iconPath);
//string checkPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +@"\1.png";
//File.WriteAllBytes(checkPath, pic);
return pic;
}
}
.CS:
emp_img.ImageUrl = "ImgHandler.ashx?FileName=1.jpg";
web.config:
<add key="ProfilePhotoPath" value="\\\\192.168.10.245\\Pic\\"/>
推荐阅读
- c# - C# 在 appdomain 调用方法中加载 dll 不会再次加载 dll
- java - 删除日历图标
- flutter - 如何通过 FlutterView 或 Flutter.createFragment 使用平台通道
- java - 限制 Android 测验应用程序中的问题,我的问题列表来自 Firebase 实时数据库
- excel - 如何在 VBA 中公开变量?
- java - 接受输入直到输入正数
- javascript - 如何在 Node.js 中创建人类可读但机器不可读的文本?
- sql - 条件运行计数/累积和:寻找公式或脚本
- java - Web 应用程序数据库或地图以提高性能
- asp.net - 模拟的 UserManager 和 roleManager 方法返回 null