如何在ASP.NET Web应用程序中调用本地图片库的路径 [英] How to call out path of local pictures library in ASP.NET web applications

查看:270
本文介绍了如何在ASP.NET Web应用程序中调用本地图片库的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在c#中有一个ASP.NET Web应用程序。我已经创建了一个下载文件的方法,它工作正常。目前,它正在D驱动器中下载文件,因为我指出了路径。但是,我希望文件可以在本地图片库中下载。

以下是我的代码:



我尝试了什么:



protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)

{

if( e.CommandName ==下载)

{

Response.Clear();

Response.ContentType =application / octect-stream ;

Response.AppendHeader(content-disposition,filename =+ e.CommandArgument);

Response.TransmitFile(Server.MapPath(〜/ Images) /)+ e.CommandArgument);

WebClient webClient = new WebClient();

webClient.DownloadFile(Server.MapPath(〜/ Images /)+ e .CommandArgument,@d:\ myfile.jpg);

Response.End();





}

}

So i have a ASP.NET web app in c#. I've create a method to download files and it works fine. Currently, it's donwloading files in D drive as i pointed out that path. However, i want the files to be downloaded in Local Pictures Library.
Below are my codes:

What I have tried:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Download")
{
Response.Clear();
Response.ContentType = "application/octect-stream";
Response.AppendHeader("content-disposition", "filename=" + e.CommandArgument);
Response.TransmitFile(Server.MapPath("~/Images/") + e.CommandArgument);
WebClient webClient = new WebClient();
webClient.DownloadFile(Server.MapPath("~/Images/") + e.CommandArgument, @"d:\myfile.jpg");
Response.End();


}
}

推荐答案

Y你可以查看Environment.SpecialFolders:



Environment.SpecialFolder Enum(System)| Microsoft Docs [ ^ ]



这里有详细的详细说明:在.NET中获取所有特殊文件夹 [ ^ ]
You could look into Environment.SpecialFolders:

Environment.SpecialFolder Enum (System) | Microsoft Docs[^]

There's quite a detailed write up here: Getting All "Special Folders" in .NET[^]


Quote:

WebClient webClient = new WebClient();
webClient.DownloadFile(Server.MapPath("~/Images/") + e.CommandArgument, @"d:\myfile.jpg");


该代码在服务器上运行 。它正在将图像复制到驱动器的根目录 D: 在服务器上



在Visual Studio中调试代码时,它可能出现。但这只是因为,在特定情况下,客户端和服务器是同一台计算机。



一旦将代码部署到真实服务器,你就' ll看到文件正在保存在服务器上,而不是保存在客户端上。



其余代码是将文件发送到客户端的唯一方法:


That code is running on the server. It is copying the image to the root of drive D: on the server.

It might appear to work when you debug the code in Visual Studio. But that's only because, in that specific case, the client and the server are the same computer.

As soon as you deploy your code to a real server, you'll see that the file is being saved on the server, not on the client.

The rest of your code is the only way to send a file to the client:

Response.Clear();
Response.ContentType = "application/octect-stream";
Response.AppendHeader("content-disposition", "filename=" + e.CommandArgument);
Response.TransmitFile(Server.MapPath("~/Images/") + e.CommandArgument);
Response.End();



注意:将提示用户选择他们要保存文件的位置。无法控制用户保存文件的位置。


NB: The user will be prompted to select where they want to save the file. There is no way to control where, or even if, the user saves the file.


这篇关于如何在ASP.NET Web应用程序中调用本地图片库的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆