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.GetByteCount(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;
}