2

I am trying the following, as seen here on SO:

       System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;

        response.ClearContent();
                            response.Clear();
                            response.ContentType = "video/mp4";
                            response.AddHeader("Content-Type", "video/mp4");
                            response.AddHeader("Content-Disposition", "attachment; filename=" + filename + "; ");
         response.WriteFile(sourcepath + filename);
   response.End();

But no dialog opens and I have no idea where it saves the file.

Thanks

Aa Yy
  • 1,702
  • 5
  • 19
  • 34
  • What browser is it? Does this happen on other browsers too? – shahkalpesh Jul 16 '18 at 10:07
  • @shahkalpesh Only tried in Chrome – Aa Yy Jul 16 '18 at 10:07
  • The code you show should not open a dialog, it will just save to the sourcepath specified by you in the code, if you want a dialog to pop up i think you need something like https://stackoverflow.com/questions/19972266/download-file-from-web-and-then-save-with-a-save-file-dialog-box – Armando Bracho Jul 16 '18 at 10:12
  • 1
    BTW this code is not gonna always open a save dialog box. To do so, replace `"video/mp4"` with `"application/octet-stream"`. – Xaqron Jul 16 '18 at 10:20
  • you maybe have a bug and there is nothing send - or if you use firefox and this is a video (mp4) its save it automatically - check your browser settings – Aristos Jul 16 '18 at 10:21
  • Armando - if the browser is going to save a file, which is what this code is attempting to do, it *should* be prompting rather than trusting the server to specify where the file should be stored. I can't think of any modern browser which would trust a server-supplied path. – Damien_The_Unbeliever Jul 16 '18 at 10:21
  • The `filename` might have the wrong [encoding](https://stackoverflow.com/questions/93551/how-to-encode-the-filename-parameter-of-content-disposition-header-in-http), so check whether this is the case. – ViRuSTriNiTy Jul 16 '18 at 10:22
  • Surely it just follows the usual download protocol that the browser provides. For example in most browsers it saves to "downloads" and shows it in a ribbon at the bottom of the browser. Its definitely a browser specific thing which you won't be able to change from server side code. – Nick.Mc Jul 16 '18 at 10:23
  • I'm afraid nothing is working.. – Aa Yy Jul 17 '18 at 12:07

1 Answers1

0

I think you have two options:

  1. Changing the Content/Type to "application/octet-stream" (as said by @Xaqron).
  2. Using the HTML5 download attribute if you are using an anchor tag or something like that in your HTML to redirect to the download.
piraces
  • 1,320
  • 2
  • 18
  • 42