WebClient for downloading and uploading file

S Abijith 346 Reputation points
2021-01-05T05:13:20.263+00:00

Hi All,
We are trying to download and upload a file to a device using HTTPS protocol using the 'WebClient' class from C#. It uses .Net framework 4.5. Once we download to the device or upload a file from the device, we get a JSON response from the device which we need to capture. The problem is that the method 'DownloadFile' does not return any response and the method 'UploadFile' returns a byte array.
Is there any way that we can receive a JSON response using the 'DownloadFile' and 'UploadFile' methods present in the 'WebClient' class?

Please let us know about this. Thank you in advance.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,692 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,135 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Abdulhakim M. Elrhumi 351 Reputation points
    2021-01-13T21:22:59.187+00:00

    Hi

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class _Default : System.Web.UI.Page
    {
    #region Page Load
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    #endregion
    
    #region Upload Button Code
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {
            // Get the HttpFileCollection
            HttpFileCollection hfc = Request.Files;
            if (hfc.Count < 5)
            {
                for (int i = 0; i < hfc.Count; i++)
                {
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.ContentLength > 0)
                    {
                        hpf.SaveAs(Server.MapPath("MyFiles") + "\\" +
                          System.IO.Path.GetFileName(hpf.FileName));
                        Response.Write("<b>File: </b>" + hpf.FileName + "  <b>Size:</b> " +
                            hpf.ContentLength + "  <b>Type:</b> " + hpf.ContentType + " Uploaded Successfully <br/>");
                    }
                }
            }
            else
            { }
        }
        catch (Exception ex)
        {
    
        }
    }
    #endregion
    

    }

    Best Regards.

    Please click the Mark as answer button and vote as helpful if this reply solves your problem.

    0 comments No comments