How to save file within Ajax

Last post 02-13-2008 4:21 AM by Jin-Yu Yin - MSFT. 3 replies.

Sort Posts:

  • How to save file within Ajax

    02-10-2008, 1:13 AM
    • Participant
      1,560 point Participant
    • mycwcgr
    • Member since 08-07-2005, 8:47 AM
    • Posts 746

    The code below can't run under Ajax, could you modify it for me? Thanks!

     

            public static void SaveFile(string FileName, byte[] ss)
            {
                long fileSize = ss.LongLength;

                if (fileSize >= 1)
                {
                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.ContentType = "application/octet-stream";
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + FileName + "\"");
                    HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
                    HttpContext.Current.Response.BinaryWrite(ss);
                    HttpContext.Current.Response.End();
                }
            }

  • Re: How to save file within Ajax

    02-10-2008, 11:21 AM
    • All-Star
      17,453 point All-Star
    • albertpascual
    • Member since 05-23-2003, 2:11 PM
    • Riverside, CA
    • Posts 3,474
    • TrustedFriends-MVPs

    You can use System.IO.File.Save to save a file.

    Cheers
    Al
    My Blog
    MapStats.NET
    Please click on 'Mark as Answer' if this post answered your question!
  • Re: How to save file within Ajax

    02-10-2008, 8:55 PM
    • Participant
      1,560 point Participant
    • mycwcgr
    • Member since 08-07-2005, 8:47 AM
    • Posts 746

    Thanks! 

    I use  public static void SaveFile(string FileName, byte[] ss) to download remote data and save it to local file!

    If I use System.IO.File.Save, does it mean that first save data in remote server, then create a URL and let user to download ? If so, it's not my need.

  • Re: How to save file within Ajax

    02-13-2008, 4:21 AM
    Answer

    Hi,

    Thank you for your post!

    The only solution I think is using Iframe.

    Then what you need to do is change the Iframe's src during/after the AJAX postback, then do the save opration on the load event of the Iframe's content page.

    Please refer to http://encosia.com/2007/02/23/ajax-file-downloads-and-iframes/ to get more information.

    If you have further questions,let me know!

    Best Regards,

    Sincerely,
    Jin-Yu Yin
    Microsoft Online Community Support
Page 1 of 1 (4 items)