我已经查看了超过100篇关于文件上传的帖子,并且在找不到我想要的内容时感到非常困惑。
客户端非常明确:我想使用ajax / json将PUT或POST文件(以最好者为准)发送到服务器。我想用post发送路径和文件名,因此在查询中指定了这个,如:
$.ajax({
url: "/myService/PostFile",
data: "fileName=" + this.fileName() +
"& path=" + this.filePath() +
"& file=" + this.datafile,
dataType: 'json',
async: true,
success: function () {
this.filePosted(true);
}
});
this.fileName(),this.filePath()是ko.observables。
然而,Serverside是我最头痛的问题。我需要定义一个serviceController,调用: [AllowAnonymous]
[HttpPut]
public HttpResponseMessage PostFile(string name, string path, byte[] file data)
{
var filename = HttpContext.Current.Server.MapPath("~/" + path + "/" + filename);
// Then something like:
FileStream fs = File.OpenWrite(filename);
fs.Write(filedata, 0, filedata.Length);
fs.Close();
return;
}
我觉得我在这里很薄,我在寻求方向。 提前谢谢!