镜像在服务器上的文件路径
本文关键字:文件 路径 服务器 镜像 | 更新日期: 2023-09-27 18:14:45
我有一个图像保存在我的服务器上,我想在客户端上显示。
EDIT:我从多达80个的列表中选择一个图像。然后,如果需要,将调整该图像的大小并返回给客户端。
我的服务器运行在IIS7 @ localhost:1337 .
服务器文件位置:
C: ' inetpub ' wwwroot ' API网络' ' 4076 ' ' img '大小' jpg
1.
这是当我向客户端返回绝对路径时返回的路径(参见下面的代码)。但是客户端找不到这个文件。
我的客户端运行在IIS7 @localhost:15536.
在firebug的帮助下,我可以将客户机应用程序中Image对象的源设置为服务器上localhost下文件的位置。localhost: 1337/网络/4076/1/img/大小/jpg
1.
然后正确显示图像。
我要做哪些更改才能使我手动进行的更改自动发生?我如何创建/返回第二个链接,并在客户端使用它反对第一个链接?
服务器API调用
/// <summary>
/// Method to retrieve files from the server. Files will be searched in the requested map depending on size.
/// The resized image will be saved on the server and the location will be send to the client.
/// </summary>
/// <returns>A response message with the location of the newly resized file.</returns>
public HttpResponseMessage getFileResized(string name, int collectionId, int maxWidth, int maxHeight, int version = 1)
{
// create real file path
string basePath = FileService.GetPath(collectionId, CollectionType.collection, version) + @"'img'"; //HostingEnvironment.MapPath("~/Cyber/" + collectionId + "/img/");
string filePath = basePath + @"resized'";
// Standard location of the file when it's uploaded to the server.
string fileBase = basePath + name;
// New location for the resized file on the server.
string fileExact = filePath + name;
try
{
if (!File.Exists(filePath))
{
// create new directories for resizes
Directory.CreateDirectory(filePath);
}
if (//File.Exists(fileBase)) &&
!File.Exists(fileExact))
{
Logger.log("File found but resize missing. Creating resized...");
ImageService.createResizedFile(name, basePath, maxWidth, maxHeight);
}
// check if path and file exist
string file = Directory.GetFiles(filePath, name, SearchOption.TopDirectoryOnly).FirstOrDefault();
if (file != null)
{
// retrieve the file location, write headers and return it
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Accepted);
response.Headers.Location = new Uri(file);
return response;
}
else
{
// file does not exist at the selected location
Logger.log("Resized image file does not exist on location: {0}.", fileExact);
throw new HttpResponseException(HttpStatusCode.NotFound);
}
}
catch (DirectoryNotFoundException)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
}
客户端像这样检索文件位置
HttpResponseMessage responseMessage = client.GetAsync("api/file/getFileResized?name=" + fileName + "&collectionId=" + CollectionId
+ "&maxWidth=" + maxWidth + "&maxHeight=" + maxHeight + "&version=" + Version).Result;
string sourceResponse = "";
if (responseMessage.IsSuccessStatusCode)
{
sourceResponse = responseMessage.Headers.Location.AbsolutePath;
return Json(new { OK = 1, message = sourceResponse, refresh = true }, "text/html");
}
使用javascript和Jquery将源代码放入image src
$("#editorModal").on("shown.bs.modal", function () { showImage(); })
function showImage() {
console.log("ShowImage resizedSource");
console.log(resizedSource);
$("#selectedImage").attr("src", resizedSource);
}
resizedSource在此处理程序中设置
function getResizedImage() {
$.ajax({
url: "/NextprintPhotobook/GetResizedImageFile",
type: "POST",
data: JSON.stringify({ imageSource: imageSource }),
dataType: "json",
contentType: 'application/json; charset=utf-8',
success: function (data) {
if (data.OK != 1) {
showError("Er is een fout opgetreden bij het openen van de foto. Data niet OK.", data.message, data.refresh);
}
console.log("getResizedImage data.message");
console.log(data.message);
resizedSource = data.message;
},
error: function (data) {
showError("Er is een fout opgetreden bij het openen van de foto.");
}
});
}
只需将图像路径保存在web的<appSettings>
块中。关于服务器的配置
<add key="ImagePath" value="localhost:1337/Cyber/4076/1/img/resized/" />
然后从这个键中获取路径和从数据库中获取图像名称。最后创建如下URL:
ImageUrl = ConfigurationManager.AppSettings["ImagePath"]+ ImageName;
其中ImageName
为从数据库中提取的图像名称。将ImageUrl
返回给客户端,即
localhost:1337/Cyber/4076/1/img/resized/1.jpg
当ImageName=1.jpg
或者您也可以对动态应用程序路径
执行以下操作var context = HttpContext.Current;
string appPath = string.Format("{0}://{1}{2}{3}",
context.Request.Url.Scheme,
context.Request.Url.Host,
context.Request.Url.Port == 80 ? string.Empty : ":" + context.Request.Url.Port,
context.Request.ApplicationPath);
并使用此appPath动态设置到本地主机的相对路径。