Everyone. I am having a problem while posting WebRequest with form data and files. I have WCF service that have one method called as "SaveFileAndData". I can not change "SaveFileAndData" method body. This method is called from aspx page on DoSomething click event.
Right now i am able to post form data to wcf service method but i am not able to post file with form data. I dont know how to post a file with form data.
In service method we can get form data using HttpContext.Current.Request.Form["id"] and files HttpContext.Current.Request.Files["image1"]
I searching solution for this isuss from last three days but not getting any useful solution.
Posted files should get by HttpContext.Current.Request.Files["image1"]
This is my first question on stackoverflow and i dont know how to post question.
Thanks
OperationContract()]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "SaveFileAndData")]
void SaveFileAndData();
public void SaveFileAndData()
{
string ext1 = "";
string ext2 = "";
string filename1 = "";
string filename2 = "";
long id = 0;
if (HttpContext.Current.Request.Form["id"] != null && HttpContext.Current.Request.Form["id"].ToString().Trim() != "")
id = Int64.Parse(HttpContext.Current.Request.Form["id"].Trim());
int cid = 0;
if (HttpContext.Current.Request.Form["cid"] != null && HttpContext.Current.Request.Form["cid"].ToString().Trim() != "")
cid = Int32.Parse(HttpContext.Current.Request.Form["cid"].Trim());
HttpContext context = HttpContext.Current;
if (context != null)
{
if (context.Request.Files.Count >= 1)
{
HttpPostedFile file1 = HttpContext.Current.Request.Files["image1"];
if (file1 != null)
{
ext1 = System.IO.Path.GetExtension(file1.FileName);
filename1 = custregid + ext1;
if (((ext1.ToLower() != ".jpg") & (ext1.ToLower() != ".jpeg") & (ext1.ToLower() != ".gif") & (ext1.ToLower() != ".png") & (ext1.ToLower() != ".bmp")))
{
// Perform some operation
}
}
HttpPostedFile file2 = HttpContext.Current.Request.Files["image2"];
if (file2 != null)
{
ext2 = System.IO.Path.GetExtension(file2.FileName);
filename2 = custregid + ext2;
if (((ext2.ToLower() != ".jpg") & (ext2.ToLower() != ".jpeg") & (ext2.ToLower() != ".gif") & (ext2.ToLower() != ".png") & (ext2.ToLower() != ".bmp")))
{
// Perform some operation
}
}
}
}
}
protected void btnDoSomeThing_OnClick(object sender, EventArgs e)
{
try
{
long id = 1;
long cid = 1;
string key = "key";
WebRequest webrequest1 = WebRequest.Create("http://localhost:65010/API/Service.svc/SaveFileAndData");
webrequest1.Method = "POST";
webrequest1.Headers.Add("key", key);
string postData = "";
postData = "id=" + id + "&cid=" +cid;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
webrequest1.ContentType = "application/x-www-form-urlencoded";
webrequest1.ContentLength = byteArray.Length;
Stream dataStream = webrequest1.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
// Get the response.
WebResponse webresponse = webrequest1.GetResponse();
Stream strresponse = webresponse.GetResponseStream();
StreamReader strreader = new StreamReader(strresponse);
string responseFromServer = strreader.ReadToEnd();
webresponse.Close();
strresponse.Close();
strreader.Close();
}
catch (Exception ex)
{
throw ex;
}
}