1

I have a C# windows form application which downloads file from a url(asp.net application) but it is not returning full image lets say image is of 780kb the file that windows form creates is 381 bytes exactly.

I am not able to figure out the issue. Please help.

The code i am using for download is:

 public bool getFileFromURL(string url, string filename)
        {
            long contentLength = 0;
            Stream stream = null;
            try
            {
                WebRequest req = WebRequest.Create(url);
                WebResponse response = req.GetResponse();
                stream = response.GetResponseStream();
                contentLength = response.ContentLength;


                // Transfer the file
                byte[] buffer = new byte[10 * 1024]; // 50KB at a time
                int numBytesRead = 0;
                long totalBytesRead = 0;
                using (FileStream fileStream = new FileStream(filename, FileMode.Create))
                {
                    using (BinaryWriter fileWriter = new BinaryWriter(fileStream))
                    {
                        while (stream.CanRead)
                        {
                            numBytesRead = stream.Read(buffer, 0, buffer.Length);
                            if (numBytesRead == 0) break;
                            totalBytesRead += numBytesRead;
                            fileWriter.Write(buffer, 0, numBytesRead);
                        }
                        fileWriter.Close();
                    }
                    fileStream.Close();
                }
                stream.Close();
                response.Close();
                req.Abort();

                return true;

            }
            catch (Exception)
            {

                return false;
            }



        }

This is my asp.net app code:

   using (PortalEntities db = new PortalEntities())
        {
                PortalModel.Command command = db.Commands.SingleOrDefault(c => c.id == id);

                var filePath = Server.MapPath("~/Uploads/"+command.arguments);
                if (!File.Exists(filePath))
                    return;

                var fileInfo = new System.IO.FileInfo(filePath);
                Response.ContentType = "image/jpg";
                Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", filePath));
                Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                Response.WriteFile(filePath);
                Response.End();
        }
Deep Sharma
  • 3,374
  • 3
  • 29
  • 49

3 Answers3

1

That's an awful lot of code to write some bytes out to a file from a web response. How about something like this (.NET 4+):

public static bool GetFileFromURL(string url, string filename)
{
    try
    {
        var req = WebRequest.Create(url);
        using (Stream output = File.OpenWrite(filename))
        using (WebResponse res = req.GetResponse())
        using (Stream s = res.GetResponseStream())
            s.CopyTo(output);
        return true;
    }
    catch
    {
        return false;
    }
}
Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
  • still the file i received is of 381 bytes. Can there be something wrong with the asp.net code? – Deep Sharma Sep 30 '14 at 14:40
  • 1
    Are you out of disk space? Are you getting any exceptions from the above method (try removing the `try/catch` block)? – Cᴏʀʏ Sep 30 '14 at 14:51
0

You can download image in more elegant way, it was discussed before here Unable to locate FromStream in Image class

And use File.WriteAllBytes Method to save the byte array as a file, more info at http://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes(v=vs.110).aspx

So all your client code can be replaced with

public void getFileFromURL(string url, string filename)
{
    using (var webClient = new WebClient())
    {
        File.WriteAllBytes(filename,webClient.DownloadData(url));
    }
}
Community
  • 1
  • 1
Sergey Malyutin
  • 1,484
  • 2
  • 20
  • 44
0

Dude, why are you not using WebClient.DownloadFileAsync?

private void DownloadFile(string url, string path)
{
    using (var client = new System.Net.WebClient())
    {
        client.DownloadFileAsync(new Uri(url), path);
    }
}

That's pretty much it, but this method can't download over 2GB. But i don't think the image is that big xD.

Hope it helps!

mrphil2105
  • 21
  • 4