从其他服务器访问图像
我将图像文件放在一台服务器上,将应用程序放在其他服务器上。
我想访问该图像,在我编写的代码下面:
在default.aspx上,我有
在GetImage.aspx上,我在page_load上编写了以下代码
protected void Page_Load(object sender, EventArgs e) { // Changing the page's content type to indicate the page is returning an image Response.ContentType = "image/jpg"; var imageName = Request.QueryString["imgName"]; var path = "//SERVER/FOLDER/" + imageName; if ((string.IsNullOrEmpty(imageName) == false)) { // Retrieving the image System.Drawing.Image fullSizeImg; fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(path)); // Writing the image directly to the output stream fullSizeImg.Save(Response.OutputStream, ImageFormat.Jpeg); // Cleaning up the image fullSizeImg.Dispose(); } }
但我得到的错误
fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(path));
请让我知道我不正确的地方。 除了Server.MapPath,我还需要其他任何东西吗? 因为我的图像在其他服务器上。
编辑
- 我的电脑里有图像文件夹
- 我在其他计算机[相同网络]中创建了一个Web应用程序,部署在IIS中,图像显示正确。 使用http://10.67.XX.XX/websiteName/Default.aspx之类的路径
- 但是当我试图从我的comupter或任何其他计算机访问相同的内容时,我无法看到该图像。
您不应该使用Server.MapPath
。 这用于将站点下的虚拟路径映射到文件系统下的物理路径。 如果该文件存在于另一台服务器上,则只需直接通过名称访问它,而无需Server.MapPath
。
上面的答案是不正确的,因为图像驻留在单独的服务器上。
所以System.Images不会没有图像的位置。
其次,你必须使用server.Mappath系统映像,它需要一个Windows路径,即c:\ blah \ blah \ whatever.jpg。 你将需要使用\\ server \ C $ \ folder \ image.jpg这与system.image.FromFile配合得很好
保存你也可以利用它。
干杯