Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 下载按钮从图片库下载_Asp.net_Sharepoint_Sharepoint 2010 - Fatal编程技术网

Asp.net 下载按钮从图片库下载

Asp.net 下载按钮从图片库下载,asp.net,sharepoint,sharepoint-2010,Asp.net,Sharepoint,Sharepoint 2010,我有一个以图片库为源的ListView,每个图片都有一个代表下载按钮的asp:LinkButton。按下“我的”按钮时,应打开浏览器下载框。我使用以下代码来实现这一点: public void Download_Click(object source, EventArgs e) { LinkButton button = (LinkButton)source; string url = Server.UrlEncode(button.CommandArgument); FileI

我有一个以图片库为源的ListView,每个图片都有一个代表下载按钮的asp:LinkButton。按下“我的”按钮时,应打开浏览器下载框。我使用以下代码来实现这一点:

public void Download_Click(object source, EventArgs e)
        {
LinkButton button = (LinkButton)source;
string url = Server.UrlEncode(button.CommandArgument);
FileInfo fileInfo = new FileInfo(url);

if (fileInfo.Exists)
            {      
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
                Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.Flush();
                Response.WriteFile(fileInfo.FullName);
            }
            else
            {
                //error
            }
}
在该代码中,我使用Server.Encode(“文件名”),但我尝试了Server.Map,还使用了“PicLibraryName/fileName”和“Application/PictureLibraryName/fileName”,但我从未获得FileInfo.Exists true,因为我总是获得FileNotFoundException或System.Web.HttpException(当我使用虚拟路径时)


有人知道解决这个问题的最好方法是什么吗

我认为您应该使用
WebClient.DownloadData(url)
下载文件,然后使用
Response.OutputStream.Write
方法输出下载数据,也不要忘记使用
Response.end()
类似的方法结束响应:

var wc=new System.Net.WebClient();
var data=wc.DownloadData(url);
Response.Clear();
//add headers..
Response.OutputStream.Write(data);
Response.End();

当然可以!如果我在新文件信息(url)上使用“System.ArgumentException:URI格式不受支持”这样的绝对路径,并且使用相对路径/myPictureLibrary/pic.jpg新文件信息可以工作,我可以获得FileInfo.Name,但FileInfo.FullName仅为“\myPictureLibrary\pic.jpg”,fileInfo.Exits为false,当我尝试访问一些文件属性(如fileInfo.length)时,我得到一个System.IO.FileNotFoundException:找不到文件'/myPictureLibrary/pic.jpg'。fileInfo.FullName为“c:\myPictureLibrary\pic.jpg”,我在前面的评论中写错了!不能使用
FileInfo
类检查SharePoint库中是否存在文件。我尝试过这段代码,结果与我在之前的评论中向Marek Grzenkowicz解释的一样:FileInfo FileInfo=new FileInfo(url);var wc=new System.Net.WebClient();var data=wc.DownloadData(url);Response.Clear();Response.AddHeader(“内容处置”、“附件;文件名=“+fileInfo.Name”);Response.OutputStream.Write(数据,0,数据.长度);Response.End();当然可以。类似于:http://application:55555/PictureGallery/image.jpg(额外的空间只是为了使堆栈溢出不会显示为超链接),我也尝试了:/PictureGallery/image。jpg@Cristo您不应该使用FileInfo类,因为它设计用于文件系统,而不是url,此外,您不能使用Server.Map,因为图像存储在内容数据库中。我知道您使用它的唯一原因是为了标题中的文件名,但您只需简单地替换
filename=url.Replace(url\u of_you\u library,“”)
,或者您可以使用SPList和SPFile类,但更为复杂