public static string UploadFile (string url, httppostedfilebase file,string FieldName)
{
Stream fs = file. InputStream;
if (!fs. CanRead)
{
Return "";
}
byte[] buffer = new BYTE[FS. Length];
Fs. Read (buffer, 0, (int) fs. Length);
Fs. Close ();
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString ("x");
String PREFIX = "--", Line_end = "\ r \ n";
StringBuilder SB = new StringBuilder ();
SB. Append (PREFIX + boundary + line_end);
SB. Append ("Content-disposition:form-data; Name=\ "" + FieldName + "\"; Filename=\ "" + file. FileName + "\" "+ line_end);
SB. Append ("Content-type:" + file.) ContentType + line_end + line_end);
WebRequest request = webrequest.create (URL);
Request. Method = "POST";
Request. ContentType = "multipart/form-data;boundary=" + boundary;
Stream dataStream = Request. GetRequestStream ();
Datastream.write (Encoding.UTF8.GetBytes (sb. ToString ()), 0, Encoding.UTF8.GetByteCount (sb.) ToString ()));
Datastream.write (buffer, 0, buffer.) Length);
Datastream.write (Encoding.UTF8.GetBytes (line_end + PREFIX + boundary + PREFIX + line_end), 0, Encoding.UTF8.GetByteC Ount (line_end + PREFIX + boundary + PREFIX + line_end));
Datastream.close ();
WebResponse WebResponse = Request. GetResponse ();
Stream newstream = WebResponse.GetResponseStream ();
StreamReader RDR = new StreamReader (newstream);
var result = rdr. ReadToEnd ();
return result;
}
C # simulate Form form upload file method