I wrote that method to download file from document library. But When I enter site as annonymous and try to download file, "site.RootWeb.Folders["Dokumanlar"];" this code wants authentication.
In web.config I changed trust level like this:
<trust level="Full" originUrl=""/>
What should I do? Thanks.
private void DownloadFile() {
var docid = HttpContext.Current.Request.QueryString["id"];
int id;
int.TryParse(docid, out id);
if (id == 0) {
var response = HttpContext.Current.Response;
response.Write("Enter document id");
response.End();
return;
}
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
var dataContext = new KysDataContext(site.Url);
var docs = dataContext.Dokumanlar;
if (docs == null)
{
return;
}
var dokumanlar = docs.ToList();
var dokuman = dokumanlar.FirstOrDefault(x => x.Id == id);
if (dokuman == null)
{
var response = HttpContext.Current.Response;
response.Write("check document id");
response.End();
return;
}
SPSecurity.RunWithElevatedPrivileges(delegate()
{
var rootweb = site.RootWeb.Folders["Dokumanlar"];//this line it's want to authentication
SPFile file = rootweb.Files[dokuman.Name];
if (file != null)
{
dokuman.Hits += 1;
dataContext.SubmitChanges();
var response = HttpContext.Current.Response;
response.ClearContent();
response.ContentEncoding = Encoding.GetEncoding("windows-1254");
response.Charset = "windows-1254";
response.ContentType = "application/content-stream";
response.AppendHeader("Content-Disposition",
string.Format("attachment; filename={0}", file.Name));
response.BinaryWrite(file.OpenBinary());
response.End();
Page.ClientScript.RegisterOnSubmitStatement(typeof (Page), "closePage",
"window.onunload = CloseWindow();");
}
});
}
}