1

I am trying to upload a image to a website by using a WebRequest in C#.
The problem is that the response that I get returns an error.

I would appreciate it if someone could help me find what I did wrong.

the error:

Notice: Undefined index: qqfile in /var/www/2n4u/pomf/new/qqFileUploader.php on line 71
{"error":"File is empty.","uploadName":null}

and my code:

scrot.Save("pomfse.png", ImageFormat.Png);
FileInfo fi = new FileInfo("pomfse.png");

string qquid = "qquid=" + Guid.NewGuid();
string qqtotalfilesize = "&qqtotalfilesize=" + Convert.ToString(fi.Length);
string qqfile = "&qqfile=pomfse.png";
string url = @"http://www.pomf.se/new/up.php?" + qquid + qqtotalfilesize + qqfile;

System.Net.WebRequest req = WebRequest.CreateHttp(url);

req.ContentType = "multipart/form-data";
req.Method = "POST";

byte[] bytes = (byte[])new ImageConverter().ConvertTo(scrot, typeof(byte[]));
req.ContentLength = bytes.Length;

Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();

WebResponse resp = req.GetResponse();

StreamReader sr = new StreamReader(resp.GetResponseStream());
string response = sr.ReadToEnd().Trim();

EDIT: Seems like this question was marked as a duplicate, but the duplicate contains information for the PHP error, but there is no answer that can help me with the problem in C#.

  • I don't think this is really a dupe of the target question; it's definitely a dupe of another question. The problem here is that he's trying to do a PUT with the POST method. To use the multipart/form-data content type, he needs to construct the POST body using the proper multipart boundary strings and the proper names (e.g. uploadName) for each of the parts. – EricLaw May 15 '13 at 15:20
  • Here's a better dupe: http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data – EricLaw May 15 '13 at 15:21
  • I have tried the methods provided in that question, but I still get the same response. – Mitchell Van Manen May 15 '13 at 15:36
  • I recommend setting up FIDDLER http://fiddler2.com/ -- upload the file manually with your browser and examine the traffic with fiddler. Then upload the file with your c# code and examine the traffic with fiddler. In the difference between the 2 methods, you will find your answer. – mikey May 15 '13 at 16:12

0 Answers0