Answered by:
How can User download a ZIP file from button event?

Question
-
User2035381988 posted
HI,
I have a sql data table named STATUS with Id, int - Primary key, CustomerId, string, Approved-int
When a registered User logs in using asp.net membership - their home page shows a button.
The code is C# - On the click event - I want to query the sql table STATUS and check if Approved is = TRUE
using the CustomerId as a Query parameter which is the UniqueId from membership.
if Approved column is TRUE - a ZIP file is downloaded to the User computer from a directory on the server called product.zip.
If User is not APPROVED there is a message notification saying denied!
Thank you
Sunday, April 1, 2018 3:41 PM
Answers
-
User-1780421697 posted
If you build web forms app from a template and choose membership as provider then hopefully you have login page and you can register user as well, on secure page when user is logged in then you can check the User identity property or simply check
if (Request.IsAuthenticated)
{var stream = new MemoryStream(); stream.ToDownload("{Path}/products.zip",Response);
}{Path} is where ever your file is saved, please note there is also some privileges issues so make sure that IIS user have privileges to access file.
public static class ToDownloadExtention { public static void ToDownload(this Stream stream, string fileName, HttpResponse response) { response.Clear(); response.ContentType = "application/octet-stream"; response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", fileName)); stream.CopyTo(response.OutputStream); response.End(); } }
Call this as
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, April 3, 2018 8:09 AM -
User2035381988 posted
HI Eric,
thank you,
looks good - just not sure how to write the sql code in the event.
I want to check the table and if approved column has TRUE bit then go ahead.
There is also column with the UserId of Customer which is the GUID -
so when they login the SELECT queries the TABLE by getting the the logged in User key
from Membership and that is the parameter.
IF True then download can go ahead - do I use a CASE in Select statement?
thank you
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, April 3, 2018 8:51 AM
All replies
-
User-1838255255 posted
Hi LebsAc,
According to your description, I know you want to know how to download file from the server side or show some information when don't exist this user.
About how to download the zip file from the server side, please check this tutorial:
ASP.NET Download All Files as Zip:
https://stackoverflow.com/questions/2670263/asp-net-download-all-files-as-zip
About the code logic, you could check the following code snippet:
var boval = false; if (boval == true) { //download file code } else { ScriptManager.RegisterStartupScript(this, typeof(Page), "UpdateMsg", "alert('Don't exist this user!') ", true); }
Best Regards,
Eric Du
Tuesday, April 3, 2018 3:14 AM -
User-1780421697 posted
If you build web forms app from a template and choose membership as provider then hopefully you have login page and you can register user as well, on secure page when user is logged in then you can check the User identity property or simply check
if (Request.IsAuthenticated)
{var stream = new MemoryStream(); stream.ToDownload("{Path}/products.zip",Response);
}{Path} is where ever your file is saved, please note there is also some privileges issues so make sure that IIS user have privileges to access file.
public static class ToDownloadExtention { public static void ToDownload(this Stream stream, string fileName, HttpResponse response) { response.Clear(); response.ContentType = "application/octet-stream"; response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", fileName)); stream.CopyTo(response.OutputStream); response.End(); } }
Call this as
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, April 3, 2018 8:09 AM -
User2035381988 posted
HI Eric,
thank you,
looks good - just not sure how to write the sql code in the event.
I want to check the table and if approved column has TRUE bit then go ahead.
There is also column with the UserId of Customer which is the GUID -
so when they login the SELECT queries the TABLE by getting the the logged in User key
from Membership and that is the parameter.
IF True then download can go ahead - do I use a CASE in Select statement?
thank you
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, April 3, 2018 8:51 AM -
User-1780421697 posted
You can execute sql comman please check link below
https://www.dotnetperls.com/sqlparameter
Tuesday, April 3, 2018 10:35 AM -
User2035381988 posted
hi,
i seemed to get there with this code in the end - which checks if the logged in User exists in Datatable
and if so binds gridview with files - a button click event then enables download of the rows that are
checked.
is there any error checking you would add to the code blocks?
protected void Page_Load(object sender, EventArgs e)
{string currentUserId = Convert.ToString(Membership.GetUser().ProviderUserKey);
if (CheckStatus(currentUserId) == true)
{
if (!IsPostBack)
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Files/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
GridView1.DataSource = files;
GridView1.DataBind();
}
}
else
{Label2.Text = "You do not have products to date";
}
}
protected void DownloadFiles(object sender, EventArgs e)
{
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
zip.AddDirectoryByName("Files");
foreach (GridViewRow row in GridView1.Rows)
{
if ((row.FindControl("chkSelect") as CheckBox).Checked)
{
string filePath = (row.FindControl("lblFilePath") as Label).Text;
zip.AddFile(filePath, "Files");
}
}
Response.Clear();
Response.BufferOutput = false;
string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
zip.Save(Response.OutputStream);
Response.End();
}
}
protected bool CheckStatus(string clientid)
{string cmdText = "SELECT COUNT(*) FROM xxx WHERE CustomerId = @customerid";
string strcon = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
//create new sqlconnection and connection to database by using connection string from web.config file
SqlConnection conn = new SqlConnection(strcon);{
conn.Open(); // Open DB connection.
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
cmd.Parameters.AddWithValue("@CustomerId", clientid); // Add the SQL parameter.int count = (int)cmd.ExecuteScalar();
conn.Close();// True (> 0) when the username exists, false (= 0) when the username does not exist.
return (count > 0);
}}
}
Tuesday, April 3, 2018 10:58 AM