hello
in application i have to send xls file to another server..bt not using ftp
i trid using web client and http web request but fail to upload file..
please give solution..
sample code
if (FileUpload1.HasFile)
{
string path =
Path.GetFullPath(FileUpload1.PostedFile.FileName);
string fileName =
Path.GetFileName(FileUpload1.PostedFile.FileName);
string fileExtension =
Path.GetExtension(FileUpload1.PostedFile.FileName);
string fileLocation = Server.MapPath(@"~/upload/" +
fileName);
string URLAuth =
"http://enterprise.smsgupshup.com/GatewayAPI/rest";
//string url = "http://adityaindia.in/admin/admin/upload/"
+ fileName;
FileUpload1.SaveAs(fileLocation);
//string localFile = "C:\\Program Files\\Common
Files\\Microsoft Shared\\DevServer\\10.0\\sample.xls";
WebClient wbClient = new WebClient();
NameValueCollection formData = new NameValueCollection();
formData["method"] = "xlsUpload";
formData["userid"] = "XXXXXXXXX";
formData["password"] = "XXXXXX";
formData["v"] = "1.1";
formData["filetype"] = "xls";
//formData["msg_type"] = "Text";
formData["auth_scheme"] = "PLAIN";
formData["xlsfile"] = fileLocation;
UploadFilesToRemoteUrl(URLAuth, fileLocation, fileLocation,
formData);
}
public static void UploadFilesToRemoteUrl(string url, string files,
string logpath, NameValueCollection nvc)
{
long length = 0;
string boundary = "----------------------------" +
DateTime.Now.Ticks.ToString("x");
//string boundary = "ou812--------------8c405ee4e38917c";
HttpWebRequest httpWebRequest2 =
(HttpWebRequest)WebRequest.Create(url);
httpWebRequest2.ContentType = "multipart/form-data;
boundary=" + boundary;
httpWebRequest2.Method = "POST";
httpWebRequest2.KeepAlive = true;
httpWebRequest2.Credentials =
System.Net.CredentialCache.DefaultCredentials;
Stream memStream = new System.IO.MemoryStream();
byte[] boundarybytes =
System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
string formdataTemplate = "\r\n--" + boundary +
"\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
foreach (string key in nvc.Keys)
{
string formitem = string.Format(formdataTemplate, key,
nvc[key]);
byte[] formitembytes =
System.Text.Encoding.UTF8.GetBytes(formitem);
memStream.Write(formitembytes, 0, formitembytes.Length);
}
memStream.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "Content-Disposition: form-data;
name=\"{0}\"; filename=\"{1}\"\r\n Content-Type:
application/octet-stream\r\n\r\n";
string header = string.Format(headerTemplate, "uplTheFile",
files);
byte[] headerbytes =
System.Text.Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(files,
FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0,
buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
httpWebRequest2.MaximumResponseHeadersLength = 100000;
httpWebRequest2.ReadWriteTimeout = 200000;
}
memStream.Write(boundarybytes, 0, boundarybytes.Length);
fileStream.Close();
httpWebRequest2.ContentLength = memStream.Length;
Stream requestStream = httpWebRequest2.GetRequestStream();
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
WebResponse webResponse2 = httpWebRequest2.GetResponse();
Stream stream2 = webResponse2.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
string s = (reader2.ReadToEnd());
stream2.Flush();
stream2.Close();
requestStream.Flush();
requestStream.Close();
reader2.Close();
webResponse2.Close();
httpWebRequest2 = null;
webResponse2 = null;
}
catch (Exception ex)
{}
}
--
You received this message because you are subscribed to the Google
Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML
Web Services,.NET Remoting" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/dotnetdevelopment?hl=en?hl=en
or visit the group website at http://megasolutions.net