0

I have a webpart that needs to create dynamically a .zip file in the server and download that file to the client machine, once the user clicks on an Export button.

The .zip generation is ready, I just need to know how to download the file to the client machine, in a way the user chooses the destination directory. I saw 2 main different solutions over the internet: one uses the Response.TransmitFile/WriteFile, and the other one involves creating a download page. Is there a easiest way to do this directly from the code behind?

2 Answers 2

1

Assuming you have the MemoryStream, the below code will bring the Save Dialog so user can choose the path

Byte[] byteArray = memoryStream.ToArray();
memoryStream.Flush();
memoryStream.Close();
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition","attachment; filename=filename.zip");
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(byteArray);
Response.End();

Fix for non-responsive events

Change your Export button as follows

<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="Export_Click" OnClientClick="javascript:setFormSubmitToFalse()" />

Add this JavaScript function to the ASCX file

function setFormSubmitToFalse() {
    setTimeout(function () { _spFormOnSubmitCalled = false; }, 1000);
    return true;
}
3
  • Thanks Amal, but as I explained to Naim, I tried this solution before, and it freezes the webpart after the Response.End(), it only works once, so it's not acceptable for the web part.
    – tcbrazil
    Jan 24, 2015 at 1:00
  • @tcbrazil please try my fix. Jan 24, 2015 at 1:04
  • Amal, it worked! I found an excellent explanation link here on why we need to add this JavaScript function. The solution based on an exclusive download page worked as well, but as a good cartesian-minded guy, I don't like it for adding an "unnecessary" component.
    – tcbrazil
    Jan 29, 2015 at 10:04
0

In the event handler of your Export button use this code

Response.AddHeader("Content-disposition", "attachment; filename=YourFile.zip");
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(bytes); //bytes is byte[] of your zip file
Response.End();
3
  • Thanks Naim, but this is one of the solutions I tried, and it freezes the webpart after the Response.End(), it only works once, so it's not acceptable for the webpart.
    – tcbrazil
    Jan 24, 2015 at 0:51
  • I use this in application pages with no issues. As you have tested it doesn't work in web parts. Let's wait for other users ideas. Jan 24, 2015 at 0:57
  • If you don't find better solution one workaround is to have one application page just for that purpose. That page doesn't have to be shown to the users. Link Jan 24, 2015 at 1:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.