gpt4 book ai didi

javascript - 使用 ember-upload 上传文件,如何用服务堆栈的附加数据填充请求?

转载 作者:行者123 更新时间:2023-12-02 16:45:32 27 4
gpt4 key购买 nike

作为介绍,我在服务堆栈和通过 REST 用 ember.js 编写的应用程序之间进行通信时遇到问题,我正在使用 ember-uploader组件将文件上传到服务堆栈。

查看 hbs:

<table class="table table-bordered table-hover">
{{file-upload}}
</table>

CoffeeScript 中的组件

  ABC.FileUploadComponent = Ember.FileField.extend(
url: "/api/upload"
filesDidChange: (->
uploadUrl = @get("url")
console.log uploadUrl
files = @get("files")
test = { fileName: "test" }
uploader = Ember.Uploader.create(
url: uploadUrl
)

uploader.upload(files[0],test) unless Ember.isEmpty(files)
console.log files
return
).observes("files")
)

JavaScript 中的组件

ABC.FileUploadComponent = Ember.FileField.extend({
url: "/api/upload",
filesDidChange: (function() {
var files, test, uploadUrl, uploader;
uploadUrl = this.get("url");
console.log(uploadUrl);
files = this.get("files");
test = {
fileName: "test"
};
uploader = Ember.Uploader.create({
url: uploadUrl,
data: test
});
if (!Ember.isEmpty(files)) {
uploader.upload(files[0], test);
}
console.log(files);
}).observes("files")
});

我的服务模式:

namespace ABC.Service.ServiceModel
{
public class Upload
{

[Route("/upload")]
public class UploadRequest : IRequiresRequestStream
{
public System.IO.Stream RequestStream { set; get; }

public object FileName { set; get; }

}

public class UploadResponse
{
public int Successed { set; get; }

}
}
}

我的服务方式

namespace ABC.Service.Service
{
public class UploadService : ServiceBase // Service base inherites from ServiceStack.Service
{
public Upload.UploadResponse Post(Upload.UploadRequest request)
{
var req = base.Request;
var reqThatIwant = request.FileName;

return new Upload.UploadResponse() { Successed = 1 };
}

}
}

这是 watch 的屏幕:

所以我的问题是,如何更改代码才能将标记为“2”的数据获取到标记为“1”(在屏幕上标记)的请求对象?

add watch screen shoot

最佳答案

处理原始请求流

当您使用IRequiresRequestStream时,您是在说您想要take over deserializing the Request并以流的形式访问原始输入 HTTP 请求正文。因此,ServiceStack 不会尝试从请求主体中读取数据,而是注入(inject) HTTP 请求流 - 在这种情况下,它能够填充的唯一请求 DTO 参数是 /pathinfo 上的参数>?QueryString,例如:

[Route("/upload/{FileName}")]
public class Upload : IRequiresRequestStream
{
public Stream RequestStream { set; get; }

public string FileName { set; get; }
}

访问 FormData HTTP POST

但是,如果 JavaScript 组件向您发送 HTTP POST FormData(即 application/x-www-form-urlencodedmultipart/form-data),则不太可能您希望将其视为原始请求流,而是访问已发布的 Request.FormDataRequest.Files

处理文件上传示例

根据您的屏幕截图,HTTP 请求Content-Typemultipart/form-data,在这种情况下您很可能能够access any uploaded files using Request.Files .

访问 HTTP 上传文件的一些示例可在 Live Demos 中找到。 :

Imgur - Save uploaded files to a MemoryStream

public object Post(Upload request)
{
foreach (var uploadedFile in Request.Files
.Where(uploadedFile => uploadedFile.ContentLength > 0))
{
using (var ms = new MemoryStream())
{
uploadedFile.WriteTo(ms);
WriteImage(ms);
}
}
return HttpResult.Redirect("/");
}

Rest Files - Save to FileSystem

public void Post(Files request)
{
var targetDir = GetPath(request);

var isExistingFile = targetDir.Exists
&& (targetDir.Attributes & FileAttributes.Directory) != FileAttributes.Directory;

if (isExistingFile)
throw new NotSupportedException(
"POST only supports uploading new files. Use PUT to replace contents of an existing file");

if (!Directory.Exists(targetDir.FullName))
Directory.CreateDirectory(targetDir.FullName);

foreach (var uploadedFile in base.Request.Files)
{
var newFilePath = Path.Combine(targetDir.FullName, uploadedFile.FileName);
uploadedFile.SaveTo(newFilePath);
}
}

HTTP Benchmarks - Handle multiple and .zip uploaded files

public object Post(UploadTestResults request)
{
//...
foreach (var httpFile in base.Request.Files)
{
if (httpFile.FileName.ToLower().EndsWith(".zip"))
{
using (var zip = ZipFile.Read(httpFile.InputStream))
{
var zipResults = new List<TestResult>();
foreach (var zipEntry in zip)
{
using (var ms = new MemoryStream())
{
zipEntry.Extract(ms);
var bytes = ms.ToArray();

var result = new MemoryStream(bytes).ToTestResult();
zipResults.Add(result);
}
}
newResults.AddRange(zipResults);
}
}
else
{
var result = httpFile.InputStream.ToTestResult();
newResults.Add(result);
}
}
}

关于javascript - 使用 ember-upload 上传文件,如何用服务堆栈的附加数据填充请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27151823/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com