如何以编程方式从 sharepoint 文档库下载文件 [英] How to Programatically Download files from sharepoint document library

查看:75
本文介绍了如何以编程方式从 sharepoint 文档库下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在按钮单击事件或链接按钮单击时,我想从 sharepoint 文档库下载文档并将其保存到用户的本地磁盘.

On button click event or on Link button click, I want to download document from sharepoint document library and save it to the user's local disk.

请帮助我,如果您有任何代码示例,请分享

Plz help me on this,If you have any code sample then please share

推荐答案

输出文件直接链接的问题在于,对于某些内容类型,它可能只是在浏览器窗口中打开.如果这不是想要的结果,并且您想强制保存文件对话框,您需要编写一个 ASP/PHP 页面,您可以通过查询字符串将文件名传递给该页面.然后,该页面可以读取该文件并在响应中设置一些标头以指示内容处置是和附件.

The problem with outputting a direct link to the file, is that for some content types it may just open in the browser window. If that is not the desired outcome, and you want to force the save file dialog, you'll need to write an ASP/PHP page that you could pass a filename to via the querystring. This page could then read the file and set some headers on the response to indicate that the content-disposition is and attachment.

对于 ASP.net,如果您创建一个名为 download.aspx 的简单 aspx 页面,请将以下代码添加到其中,然后将此文件放在服务器上的某个位置,您可以通过调用此页面来下载文件:

For ASP.net, if you create a simple aspx page called download.aspx, add the following code into it, then put this file on a server somewhere you can download files by calling this page like this:

http://yourserveraddress/download.aspx?path=http://yoursharepointserver/pathtodoclibrary/file.ext

protected void Page_Load(object sender, EventArgs e)
    {
        string path = "";
        string fileName = "";

        path = Request.QueryString["path"];
        if (path != null && path.Length > 0)
        {
            int lastIndex = path.LastIndexOf("/");
            fileName = path.Substring(lastIndex + 1, (path.Length - lastIndex - 1));

            byte[] data;
            data = GetDataFromURL(path);

            Response.Clear();
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
            Response.BinaryWrite(data);
            Response.Flush();
        }
    }


    protected byte[] GetDataFromURL(string url)
    {
        WebRequest request = WebRequest.Create(url);
        byte[] result;
        byte[] buffer = new byte[4096];

        //uncomment this line if you need to be authenticated to get to the files on SP
        //request.Credentials = new NetworkCredential("username", "password", "domain");

        using (WebResponse response = request.GetResponse())
        {
            using (Stream responseStream = response.GetResponseStream())
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    int count = 0;
                    do
                    {
                        count = responseStream.Read(buffer, 0, buffer.Length);
                        ms.Write(buffer, 0, count);
                    } while (count != 0);
                    result = ms.ToArray();
                }
            }
        }
        return result;
    }

这篇关于如何以编程方式从 sharepoint 文档库下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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