0

I am developing licensing for a desktop application. A simple workflow:

  1. User's details are submitted via HttpPost method to server (or other)
  2. Server checks details and generates files.
  3. Server sends a file to user (as a confirmation)

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
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user3599495
  • 107
  • 8
  • 1
    For that kind of scenario, you want to implement **Web Service** *(such as Web API)* instead of **ASP.Net Web Form**. – Win Jan 05 '15 at 18:08
  • It is always good to show us what you have already tried. I expect there would be some tutorials on the internets... – Robert Rossmann Jan 05 '15 at 18:14

2 Answers2

1

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();
Ali
  • 1,265
  • 14
  • 18
  • Watch your MIME type - I've seen IIS not serve files without throwing errors when an unsupported type is used. – Tim Jan 05 '15 at 18:06
  • Correct me if I am wrong, but this is for response created from desktop application, i.e. the one that sends pairs of data. I need server to respond to this request with a file. – user3599495 Jan 05 '15 at 18:07
1
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();
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Mike W
  • 308
  • 3
  • 5
  • Looks good. What is the best way to handle to incoming stream at the application side? – user3599495 Jan 05 '15 at 18:12
  • Just use the same HttpRequest/Response/WebClient code to retrieve the file when you send the request! read the response. http://stackoverflow.com/questions/4088625/net-simplest-way-to-send-post-with-data-and-read-response – Ahmed ilyas Jan 05 '15 at 18:42