Reputation: 107
I am developing licensing for a desktop application. A simple workflow:
I need help with Step 3- how to send a file to user?
For example, User (HttpPost)
var client = new HttpClient();
var pairs = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("serialNumber", "AAAA"),
new KeyValuePair<string, string>("email", "test@123"),
new KeyValuePair<string, string>("HWID", "AAAA-BBBB-CCCC-DDDD"),
};
var content = new FormUrlEncodedContent(pairs);
var response = client.PostAsync("https://localhost:44302/testCom.aspx", content).Result;
if (response.IsSuccessStatusCode)
{
//Code for file receiving?
}
Server
protected void Page_Load(object sender, EventArgs e)
{
//Check license ID
//Issue license file - How to?
//Mark database
}
Upvotes: 0
Views: 959
Reputation: 318
private void sendFile(string path, string fileName)
{
FileStream fs = new FileStream(path, FileMode.Open);
streamFileToUser(fs, fileName);
fs.Close();
}
private void streamFileToUser(Stream str, string fileName)
{
byte[] buf = new byte[str.Length]; //declare arraysize
str.Read(buf, 0, buf.Length);
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
HttpContext.Current.Response.AddHeader("Content-Length", str.Length.ToString());
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.OutputStream.Write(buf, 0, buf.Length);
HttpContext.Current.Response.End();
}
Upvotes: 1
Reputation: 1295
You can use a Response class to send a file back to client
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "text/plain";
Response.Flush();
Response.TransmitFile(file.FullName);
Response.End();
Upvotes: 1