Search code examples
c#asp.net.nethttphttpresponse

Asp.net C# How to send file and message to client?


1- I would like to send a .zip folder with password to client

and

2- send the password to the bottom of the user's page.

and

3- send a warning to raise security awareness to user

In my HttpResponse, I can either send the file OR send the popup message and the password.

Thanks in advance.


// Creating zip file with password encryption.
string password = Membership.GeneratePassword(64, 10);
FastZip zip = new FastZip();
zip.EntryEncryptionMethod = ZipEncryptionMethod.AES256;
zip.Password = password;
zip.CreateZip(folderPath + ".zip", folderPath, true, "");

// Sending file to user.
Response.ContentType = "Application/zip";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + folderName);
Response.TransmitFile(Server.MapPath("~/App_Data/Temp/" + folderName));

// Sending password and warning to user (popup).
Response.Write("<script>alert('Avertissement : S’il vous plaît, éviter de conserver les documents confidentiels sur votre poste afin d’éviter la compromission d’informations identifiables personnels (PII) d\\'employés.');</script>");

// Send password to user page (on html page).
litMsgWm.Text = "Utiliser le mot de passe: " + password + " <br> et le logiciel '7Zip' pour décrypter le fichier zip envoyé à votre navigateur.";

What I want to send:

1- the password enter image description here

2- the security awareness warning enter image description here

3- the zip file

enter image description here

I tried:

Sending the file and the password through the same http response. I dont know if we can send 2 Http responses one after the other, or just one at the time? Ideally, I would like to send two separate Http responses to the client. I tried it but it doesn't work. (I'm not the best with Http)


Solution

  • Finally, I used OnClientClick() which is called first, on the client browser, to show the message. Then I used OnClick() to launch the file download, on the server.

    <asp:Button id="btnDownload" Text="Télécharger" Title="Télécharger le fichier sélectionné" CssClass="btn_cover" OnClientClick="ShowSecAwarenessMessage();" OnClick="Download_Click" runat="server" />