简体   繁体   中英

How to download Document programmatically in SharePoint?

I wanted to have a link when user clicked, it allow user to download document from SharePoint Document Library.

The link will be placed inside an aspx page.

I tried the function below:

Response.ContentType = "application/xls";
Response.AppendHeader("Content-Disposition", "attachment; filename=abc.xls");
Response.TransmitFile(Server.MapPath("~/abc.xls"));
Response.End();

It allow me to download the document successfully, but the downloaded document cannot open.

what is the better way to do this?

Appreciate if you could provide me some references, thank you.

For your title, Javascript can get the contents of a web.config through an XMLHTTPRequest to the server, where the server is sending that file. Of course, that's a security risk and I can't think of any reason a person would want to do that.

As for you Excel file, I would suggest using a Content-Type of "application/octet-stream" for binary responses. The browser will then simply save the binary content under the specified file name and let the program (in this case, Excel) take care of interpretting it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM