i am uploading image using wcf and jquery it's show me 400 Bad request error,
[DataContract]
public class File
{
[DataMember]
public string fileName { get; set; }
[DataMember]
public Stream stream { get; set; }
}
[WebInvoke(Method = "*", UriTemplate = "UploadFile", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
void UploadFile(File file);
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)
{
}
}
$("#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',
type: 'POST',
data: fileData,
cache: false,
processData: false,
contentType: "application/octet-stream",
success: function (data) {
alert('Successful..');
},
error: function (data) {
alert('Error Occurred');
}
});
});