c# - File Type Not Supported -
im trying send image remote server i'm taking current program. how i've done first image must saved user's workstation, i'm storing location , passing method in order pass server.
however, when run code pass image along error saying file type not supported.
here run down of code:
public static void httpuploadfile(string url, string file, string paramname, string contenttype, namevaluecollection nvc) { console.write(string.format("uploading {0} {1}", file, url)); string boundary = "---------------------------" + datetime.now.ticks.tostring("x"); byte[] boundarybytes = system.text.encoding.ascii.getbytes("\r\n--" + boundary + "\r\n"); httpwebrequest wr = (httpwebrequest)webrequest.create(url); wr.contenttype = "multipart/form-data; boundary=" + boundary; wr.method = "post"; wr.keepalive = true; wr.credentials = system.net.credentialcache.defaultcredentials; stream rs = wr.getrequeststream(); string formdatatemplate = "content-disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; foreach (string key in nvc.keys) { rs.write(boundarybytes, 0, boundarybytes.length); string formitem = string.format(formdatatemplate, key, nvc[key]); byte[] formitembytes = system.text.encoding.utf8.getbytes(formitem); rs.write(formitembytes, 0, formitembytes.length); } rs.write(boundarybytes, 0, boundarybytes.length); string headertemplate = "content-disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\ncontent-type: {2}\r\n\r\n"; string header = string.format(headertemplate, paramname, file, contenttype); byte[] headerbytes = system.text.encoding.utf8.getbytes(header); rs.write(headerbytes, 0, headerbytes.length); filestream filestream = new filestream(file, filemode.open, fileaccess.read); byte[] buffer = new byte[4096]; int bytesread = 0; while ((bytesread = filestream.read(buffer, 0, buffer.length)) != 0) { rs.write(buffer, 0, bytesread); } filestream.close(); byte[] trailer = system.text.encoding.ascii.getbytes("\r\n--" + boundary + "--\r\n"); rs.write(trailer, 0, trailer.length); rs.close(); webresponse wresp = null; try { wresp = wr.getresponse(); stream stream2 = wresp.getresponsestream(); streamreader reader2 = new streamreader(stream2); console.write(string.format("file uploaded, server response is: {0}", reader2.readtoend())); } catch (exception ex) { console.write("error uploading file", ex); if (wresp != null) { wresp.close(); wresp = null; } } { wr = null; } }
i'm calling method so:
namevaluecollection nvc = new namevaluecollection(); nvc.add("id", "ttr"); nvc.add("btn-submit-photo", "upload"); httpuploadfile("http://websitelocation.com/images/uploadimage.html", sfd.tostring(),"image", "image/jpeg", nvc);
in above method sfd relates save file dialog use save image. error this:
the given path's format not supported.
which highlighted in line:
filestream filestream = new filestream(file, filemode.open, fileaccess.read);
i thought above code fine send file along why i'm confused happening.
can see reason why? i've spent while looking @ , think fresh pair of eyes , brain needed.
for sake of completion string when call sfc.tostring()
this:
system.windows.forms.savefiledialog: title: filename: c:\users\mycomputer\desktop\img.png
that exception message isn't complaining format of file. it's complaining format of file name. have invalid characters in path.
in code before try open file, write this:
if (file.indexofany(path.getinvalidpathchars()) >= 0) { throw new argumentexception("file name contains invalid characters", "file"); }
if identifies problem, best bet fix before call method. perhaps can strip offending characters or convert them underlines. without knowing more how program works, it's tough make recommendations.
Comments
Post a Comment