Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am uploading image using wcf and jquery it's show me 400 Bad request error,

C#
[DataContract]
   public class File
   {
       [DataMember]
       public string fileName { get; set; }
       [DataMember]
       public Stream stream { get; set; }
   }


C#
[WebInvoke(Method = "*", UriTemplate = "UploadFile", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
       [OperationContract]
       void UploadFile(File file);



C#
public void UploadFile(File files)
       {
           try
           {
               string FilePath = "";
               string FileName = "";
               string[] file = files.fileName.Split('.');
               if (file[1].ToLower().Equals("png") || file[1].ToLower().Equals("jpg"))
               {
                    FileName = string.Format(@"{0}." + file[1], Guid.NewGuid());
                   FilePath = Path.Combine(HostingEnvironment.MapPath("~/Files/Image"), FileName);
               }
               else
               {
                    FileName = string.Format(@"{0}.pdf", Guid.NewGuid());
                   FilePath = Path.Combine(HostingEnvironment.MapPath("~/Files/Uploads"), FileName);

               }

               int length = 0;
               using (FileStream writer = new FileStream(FilePath, FileMode.Create))
               {
                   int readCount;
                   var buffer = new byte[8192];
                   while ((readCount = files.stream.Read(buffer, 0, buffer.Length)) != 0)
                   {
                       writer.Write(buffer, 0, readCount);
                       length += readCount;
                   }
               }
           }
           catch (Exception ex)
           {

           }
       }



JavaScript
$("#btnUpload").click(function () {
                var data = new FormData();
                fileData = document.getElementById("fileUpload").files[0];
                var param={fileName:fileData.name}
               
                $.ajax({
                    url: 'http://localhost:54594/Expense.svc/UploadFile', // Dynamically uploads the files which is chosen.
                    type: 'POST',
                    data: fileData, // This would pass the file object with data
                    cache: false,
                    processData: false, // Don't process the files
                    contentType: "application/octet-stream", // Setting content type to "application/octet-stream" as jQuery will tell the server its not query string.
                    success: function (data) {
                        alert('Successful..');
                    },
                    error: function (data) {
                        alert('Error Occurred');
                    }
                });
            });
Posted
Comments
Richard Deeming 16-Jan-16 6:01am    
Try asking in the forum at the bottom of the article that this code came from:
Uploading/Downloading a file using WCF REST service in .NET 3.5[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900