c# - Upload files from client to server programmatically -
c# - Upload files from client to server programmatically -
i'm working on fileupload command simple application transfer file server automatically.
i used references samples setting file upload within webbrowser component , uploadfileex when tested doesn't create file on server!
form code:
// simulate form //<form action ="http://localhost/test.php" method = post> //<input type = text name = uname> //<input type = password name =passwd> //<input type = file name = uploadfile> //<input type=submit>
i found class post file server using httpwebrequest:
public static string uploadfileex( string uploadfile, string url, string fileformname, string contenttype,namevaluecollection querystring, cookiecontainer cookies) { if( (fileformname== null) || (fileformname.length ==0)) { fileformname = "file"; } if( (contenttype== null) || (contenttype.length ==0)) { contenttype = "application/octet-stream"; } string postdata; postdata = "?"; if (querystring!=null) { foreach(string key in querystring.keys) { postdata+= key +"=" + querystring.get(key)+"&"; } } uri uri = new uri(url+postdata); string boundary = "----------" + datetime.now.ticks.tostring("x"); httpwebrequest webrequest = (httpwebrequest)webrequest.create(uri); webrequest.cookiecontainer = cookies; webrequest.contenttype = "multipart/form-data; boundary=" + boundary; webrequest.method = "post"; // build post message header stringbuilder sb = new stringbuilder(); sb.append("--"); sb.append(boundary); sb.append("\r\n"); sb.append("content-disposition: form-data; name=\""); sb.append(fileformname); sb.append("\"; filename=\""); sb.append(path.getfilename(uploadfile)); sb.append("\""); sb.append("\r\n"); sb.append("content-type: "); sb.append(contenttype); sb.append("\r\n"); sb.append("\r\n"); string postheader = sb.tostring(); byte[] postheaderbytes = encoding.utf8.getbytes(postheader); // build trailing boundary string byte array // ensuring boundary appears on line byte[] boundarybytes = encoding.ascii.getbytes("\r\n--" + boundary + "\r\n"); filestream filestream = new filestream(uploadfile, filemode.open, fileaccess.read); long length = postheaderbytes.length + filestream.length + boundarybytes.length; webrequest.contentlength = length; stream requeststream = webrequest.getrequeststream(); // write out our post header requeststream.write(postheaderbytes, 0, postheaderbytes.length); // write out file contents byte[] buffer = new byte[checked((uint)math.min(4096, (int)filestream.length))]; int bytesread = 0; while ( (bytesread = filestream.read(buffer, 0, buffer.length)) != 0 ) requeststream.write(buffer, 0, bytesread); // write out trailing boundary requeststream.write(boundarybytes, 0, boundarybytes.length); requeststream.flush(); requeststream.close(); webresponse responce = webrequest.getresponse(); stream s = responce.getresponsestream(); streamreader sr = new streamreader(s); homecoming sr.readtoend(); } /// <summary> /// main entry point application. /// </summary> [stathread] static void main(string[] args) { cookiecontainer cookies = new cookiecontainer(); //add or utilize cookies namevaluecollection querystring = new namevaluecollection(); querystring["uname"]=""; querystring["passwd"]=""; string uploadfile;// set file upload uploadfile = "c:\\test.jpg"; uploadfileex(uploadfile, "http://127.0.0.1/app/default.aspx", "uploadfile", "image/pjpeg", querystring, cookies); }
in case want utilize class via webbrowser command when send post server file(test.jpg) not created!
it problem of permission on folder! when tested localy using iis same problem?
thanks
it's bit hard understand question, sorry if isn't right answer, if want upload file using webrequest standard post goes this: upload files httpwebrequest (multipart/form-data)
c# httpwebrequest webbrowser-control
Comments
Post a Comment