My code here works fine to get the content length, and tells me how big the file is, then it works on the if statement. Just for some reason after the if statement, if the MB is less than 5, it won’t download the file, it just stops the program in general. It’s like after the HTTP Request, it just stops. So I don’t know if it’s my codes fault, or if something else, if this is a stupid question, my apologies.
byte[] test;
int size;
long MB = 0;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Path);
req.Method = "HEAD";
HttpWebResponse resp = (HttpWebResponse)(req.GetResponse());
long len = resp.ContentLength;
MB = len / 1048576;
if (MB > 5)
{
Console.WriteLine("File to big!");
}
else
{
var webClient = new WebClient();
MemoryStream ms = new MemoryStream();
byte[] imageBytes = webClient.DownloadData(Path);
test = imageBytes;
size = test.Length;
}
Just guessing, but perhaps calling
resp.Close();prior to attempting to open another connection would help?