我正在使用 WCF Web 服务,我很好奇是否有任何方法可以将 Stream 转换为文件。 有时我在 post 方法上遇到“跨源请求错误”问题,我意识到每当我以 Stream 形式接收数据时都没有问题。 但是现在我想以同样的方式将图像发布到我的方法中(如果有办法的话)
这是我的代码:
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "SaveImage")]
public bool SaveImage(Stream streamData){
// read the streamData
// convert streamData to File
// with something like this: new FileStream(..streamData);
return true;
}
编辑:
HTML代码:
<form><input type="file" name="file"/><div id="send">send</div></form>
Jquery ajax:
$('#send').click(function () {
var allDataFromTheForm = new FormData($('form')[0]);
$.ajax({
url: "/url/SaveImage",
type: "POST",
data: allDataFromTheForm,
cache: false,
contentType: false,
processData: false,
success: function (result) {
alert(result);
}
});
});
请您参考如下方法:
手边没有引用资料,但大概是这样
using(Stream fileStream = File.CreateFile(...))
{
streamData.CopyTo(fileStream);
}
