我已经浏览了这里和整个Google的帖子,似乎无法找到解决方案。
如果可以,我想在不使用Node.js的情况下这样做。如果我不能,那么我会这样做,因为它是必需的。
我知道如何上传文件,例如:
[HttpPost]
public HttpResponseMessage PostEncryption()
{
var httpPostedFile = HttpContext.Current.Request.Files["file"];
//do some editing on the file
}
我知道如何通过get
下载文件private HttpResponseMessage Get(HttpResponseMessage response)
{
var txtString= "a bunch of random stuff";
var result = Request.CreateResponse(HttpStatusCode.OK);
result.Content = new StringContent(txtString, Encoding.UTF8, "application/txt");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "test.txt"
};
return result;
}
我要做的是将文件拉入然后进行一些处理并强制在浏览器中下载文件。这是通过Web API实现的吗?如果是这样,我将如何实现它?
这是我的代码,目前正由slaks要求,此时我只是想获得一个post方法来下载文件......
[HttpPost]
public HttpResponseMessage Post()
{
var txtString= "a bunch of random stuff";
var result = Request.CreateResponse(HttpStatusCode.OK);
result.Content = new StringContent(txtString, Encoding.UTF8, "application/txt");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "test.txt"
};
return result;
}
$('#uploadFile').on('click', function () {
var data = new FormData();
var files = $("#file").get(0).files;
// Add the uploaded content to the form data collection
if (files.length > 0) {
data.append("file", files[0]);
}
$.ajax({
type: "POST",
url: "/apifile/post",
contentType: false,
processData: false,
data: data
});
});