adding header to http response in an action inside a controller in asp.net/mvc

前端 未结 5 2104
花落未央
花落未央 2021-02-13 18:14

I am streaming data from server to client for download using filestream.write. In that case what is happening is that I am able to download the file but it does not

相关标签:
5条回答
  • 2021-02-13 18:44

    In the past I built a whitelist to allow some domains to iframe my site. Remember Google's image cache used to iframe sites as well.

    static HashSet<string> frameWhiteList = new HashSet<string> { "www.domain.com",
                                                        "mysub.domain.tld",
                                                        "partner.domain.tld" };
    
        protected void EnforceFrameSecurity()
        {
            var framer = Request.UrlReferrer;
            string frameOptionsValue = "SAMEORIGIN";
    
            if (framer != null)
            {
                if (frameWhiteList.Contains(framer.Host))
                {
                    frameOptionsValue = string.Format("ALLOW-FROM {0}", framer.Host);
                }
    
            }
    
            if (string.IsNullOrEmpty(HttpContext.Current.Response.Headers["X-FRAME-OPTIONS"]))
            {
                HttpContext.Current.Response.AppendHeader("X-FRAME-OPTIONS", frameOptionsValue);
            }
        }
    
    0 讨论(0)
  • 2021-02-13 18:44
    public FileResult DownloadDocument(string id)
            {
                if (!string.IsNullOrEmpty(id))
                {
                    try
                    {
                        var fileId = Guid.Parse(id);
    
                    var myFile = AppModel.MyFiles.SingleOrDefault(x => x.Id == fileId);
    
                    if (myFile != null)
                    {
                        byte[] fileBytes = myFile.FileData;
                        return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, myFile.FileName);
                    }
                }
                catch
                {
                }
            }
    
            return null;
        }
    
    0 讨论(0)
  • 2021-02-13 18:47

    Yes, You are doing it the wrong way try this, you should add the header inside your action not as an attribute header to your method.

    HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=" & name)
    

    or

    Request.RequestContext.HttpContext.Response.AddHeader("Content-Disposition", "Attachment;filename=" & name)
    

    Update As i understand you are making an ajax call to your controller/action which wont work for file download by directly calling an action. You can achieve it this way.

    public void Download(string name)
            {
    //your logic. Sample code follows. You need to write your stream to the response.
    
                var filestream = System.IO.File.ReadAllBytes(@"path/sourcefilename.pdf");
                var stream = new MemoryStream(filestream);
                stream.WriteTo(Response.OutputStream);
                Response.AddHeader("Content-Disposition", "Attachment;filename=targetFileName.pdf");
                Response.ContentType = "application/pdf";
            }
    

    or

        public FileStreamResult Download(string name)
        {
            var filestream = System.IO.File.ReadAllBytes(@"path/sourcefilename.pdf");
            var stream = new MemoryStream(filestream);
    
    
            return new FileStreamResult(stream, "application/pdf")
            {
                FileDownloadName = "targetfilename.pdf"
            };
        }
    

    In your JS button click you can just do something similar to this.

     $('#btnDownload').click(function () {
                window.location.href = "controller/download?name=yourargument";
        });
    
    0 讨论(0)
  • 2021-02-13 18:48

    Please take a look here.

    Following is taken from referenced website.

    public FileStreamResult StreamFileFromDisk()
    {
        string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
        string fileName = "test.txt";
        return File(new FileStream(path + fileName, FileMode.Open), "text/plain", fileName);
    }
    

    Edit 1:

    Adding something that might be more of your interest from our good ol' SO. You can check for complete detail here.

    public ActionResult Download()
    {
        var document = ...
        var cd = new System.Net.Mime.ContentDisposition
        {
            // for example foo.bak
            FileName = document.FileName, 
    
            // always prompt the user for downloading, set to true if you want 
            // the browser to try to show the file inline
            Inline = false, 
        };
        Response.AppendHeader("Content-Disposition", cd.ToString());
        return File(document.Data, document.ContentType);
    }
    
    0 讨论(0)
  • 2021-02-13 18:59

    Change:

    return RedirectToAction("Index");
    

    to:

    return File(fs, "your/content-type", "filename");
    

    And move the return statement to inside your using statement.

    0 讨论(0)
提交回复
热议问题