I have a C# Rest API method which accepts System.IO Stream object as argument and I am trying to pass a file to this C# method as a file stream using Ajax call. Please suggest how to do this? I tried two ways-
- Creating FormData object in JavaScript and
- Creating File object in JavaScript.
C# method
[WebInvoke(UriTemplate = "/{businessObjectId}/uploadAttachment?filename={filename}&caption={caption}")]
public Message UploadAttachment(string filename, string caption, Stream stream, string businessObjectId){....}
Ajax Method
$("input[name*='btnUpload']").live("click", function () {
addGlobalAttachment();
});
function addGlobalAttachment() {
var fileData = $("input[id*='fileUpload']");
var fileName = fileData[0].files[0].name;
var fileCaption = $("input[name*='txtFileCaption']").val();
var friendlyID = $(".order").html();
var fileInput = new File(fileData[0].files, fileName, { type: "text/plain", lastModified: Date(0) });
var URL = "http://localhost:62059/Order/" + friendlyID + "/uploadAttachment?filename=" + fileName + "&caption=" + fileCaption;
$.ajax({
type: 'POST',
url: URL,
data: fileInput,
processData: false,
contentType: 'application/octet-stream',
success: function (attachmentResult) {
debugger;
}
},
error: function (result) {
}
});
}