0
我想上傳一個複雜的對象到我的WCF REST服務。我這樣做是因爲它似乎是同時將Stream類型對象和其他參數上載到端點的最簡單方法。上傳一個複雜的對象到JQuery的WCF REST服務
服務:
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "Upload")]
public string upload(UploadObject uploadObject)
{
return uploadObject.stream.ToString() + " " + uploadObject.guid;
}
[DataContract]
public class UploadObject
{
[DataMember]
public Stream stream { get; set; }
[DataMember]
public string guid { get; set; }
}
JQuery的
var guid = getParameterByName("guid"); //<--gets value from query string parameter
var file = $('#btnUpload').val(); //<--value from a file input box
var uploadObject = { stream: file, guid: guid };
$.ajax({
type: "POST",
contentType: "application/json",
url: "localhost/service/Upload",
data: uploadObject,
datatype: "jsonp",
processData : false,
success: function(data){
alert(data);
},
error: function (xhr, status, error) {
alert("fail");
}
});
合同的全部目的是向服務發佈一個流和一個字符串。那麼完成這個的最好方法是什麼?另外我試圖從輸入類型文件中獲取實際的文件 – user1134179