0
[WebMethod] 
public string SaveUpload()//(string u,byte fileData) 
{
        //----------------working-----------
      string response = string.Empty;
      if (HttpContext.Current.Request.Form.AllKeys.Any())
      {
      string USERID =   HttpContext.Current.Request.Form["UploadedImage[0]"];
      string TEXT = HttpContext.Current.Request.Form["UploadedImage[1]"];
      string LINK = HttpContext.Current.Request.Form["UploadedImage[2]"];
      var FILE = HttpContext.Current.Request.Files["UploadedImage[3]"];
      string USERNAME = HttpContext.Current.User.Identity.Name;
      string newfilename = string.Empty;

            if (FILE != null)
            {
                // Validate the uploaded image(optional)
                bool imageOk = CheckImageExtension(FILE.FileName);
                if (imageOk)
                {
                    string time = SqlQuery.SelectQuery("select CURRENT_TIMESTAMP").Tables[0].Rows[0][0].ToString();
                    newfilename = FormsAuthentication.HashPasswordForStoringInConfigFile(USERID + time + FILE.FileName, "SHA1") + ".png";
                    FILE.SaveAs(Server.MapPath("~/Photos/" + newfilename));
                }
                else
                {
                    //incorrect format
                }

                Tpost objTpost = new Tpost();
                objTpost.Text = System.Web.HttpUtility.HtmlEncode(TEXT);
                objTpost.Photo1 = newfilename;// uploadPhoto.FileName;
                objTpost.ByUser = HttpContext.Current.User.Identity.Name;
                objTpost.UserID = USERID;
                objTpost.Link = System.Web.HttpUtility.HtmlEncode("http://" + LINK.Replace("http://", ""));
                objTpost.Status = 0;
                objTpost.Mode = 1;
                DataTable dt = objTpost.GetPost();
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);
                ds.Tables[0].TableName = "Customers";
                response = ds.GetXml();

                // Get the complete file path
                //var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/Photos"), FILE.FileName);

                // Save the uploaded file to "UploadedFiles" folder
                //httpPostedFile.SaveAs(fileSavePath);
            }

        }
        return response;
    }

Above is the service function written in serve.asmx

  $(document).on('click', "#pbtnupload", function (e) {
    e.preventDefault();
    var data = new FormData();
    data.append("UploadedImage[0]", arr[0]);
    data.append("UploadedImage[1]", $("#ptext").val());
    data.append("UploadedImage[2]", $("#plink").val());

    // Add the uploaded image content to the form data collection
    var files = $("#puploader").get(0).files;
    if (files.length > 0) {
        data.append("UploadedImage[3]", files[0]);
    }

    // Make Ajax request with the contentType = false, and procesDate = false
    var ajaxRequest = $.ajax({
        type: "POST",
        url: "Service/Serve.asmx/SaveUpload",
        contentType: false,
        processData: false,
        data: data,
        success: function (response) {
            BindPostTop(response);
            $("#imgup").attr("src", "");
            $("#popimg").hide();
        },
        failure: function (response) {
            // alert(response.d);
        },
        error: function (response) {
            // alert(response.d);
        }
    });
});

Above is the ajax code to call service. It works awesome on localhost but fails when published and test on live. When i change the content-type in ajax then it give success but do not upload.

  • `contentType` should be a string, and defaults to 'application/x-www-form-urlencoded; charset=UTF-8'). `false` will override the default, with unknown consequences. – Roamer-1888 Mar 15 '15 at 10:16
  • I think you can find solution in below link: [http://stackoverflow.com/questions/1354749/wcf-service-to-accept-a-post-encoded-multipart-form-data/1407139#1407139][1] [1]: http://stackoverflow.com/questions/1354749/wcf-service-to-accept-a-post-encoded-multipart-form-data/1407139#1407139 – Soleiman Sharifi Mar 15 '15 at 11:07
  • "application/x-www-form-urlencoded; charset=UTF-8". Its not working , I tried, and with the link didn't find what I needed. I think we can use contenttype = false right? – Ashutosh Rambhal Mar 15 '15 at 14:23

0 Answers0