[英]Image upload with Knockout and webapi c#
I am trying to upload an image using Knokout JS and web api. 我正在尝试使用Knokout JS和web api上传图像。 This my code
这是我的代码
<div class="row">
<div class="col-sm-4">
<h3>Send Feedback</h3>
<form data-bind="submit: sendFeedback">
<div class="form-group">
<label>Feedback</label>
<textarea class="form-control" data-bind="value: feedbackText"></textarea>
</div>
<div class="form-group">
<label>Version Id</label>
<input class="form-control" type="text" data-bind="value: versionId" />
</div>
<div class="form-group">
<label>Image</label>
<input class="form-control" type="file"
data-bind="file: {data: fileInput, name: fileName, reader: someReader}" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
</div>
I am using this custom binding 我正在使用这个自定义绑定
https://github.com/TooManyBees/knockoutjs-file-binding https://github.com/TooManyBees/knockoutjs-file-binding
Then in my script code I am doing this 然后在我的脚本代码中我这样做
self.sendFeedback = function () {
self.result('');
var feedBackData = {
versionId: self.versionId(),
text: self.feedbackText(),
screenShot: self.fileInput
};
$.ajax({
type: 'POST',
url: apiUrl + '/Feedback/Add',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(feedBackData)
}).done(function (data) {
self.result("Done!");
}).fail(showError);
}
I am uncertain about the server part of the code. 我不确定代码的服务器部分。 I have written this so far
到目前为止我写过这个
public void Add(HttpPostedFileBase screenShot, String versionId, String text)
{
String imgId = null;
int count = HttpContext.Current.Request.Files.Count;
if (screenShot != null && screenShot.ContentLength > 0)
{
Images img = Images.Create().Save();
imgId = img.Id;
BlobHelper.PutFile(imgId, screenShot.InputStream);
}
Feedback.Create(versionId, text, imgId).Save();
}
Any thoughts on how to do this? 有关如何做到这一点的任何想法?
The fileInput
contains the base64-encoded file data. fileInput
包含base64编码的文件数据。 It's a string, so HttpPostedFileBase would not work. 它是一个字符串,所以HttpPostedFileBase不起作用。
Change the form HTML: 更改表单HTML:
<input class="form-control" type="file"
data-bind="file: {data: fileInput}" />
Change the viewmodel code as follows: 更改viewmodel代码,如下所示:
// store file data here
self.fileInput = ko.observable(); // is this present already?
var feedBackData = {
versionId: self.versionId(),
text: self.feedbackText(),
screenShot: self.fileInput()
};
$.ajax({
type: 'POST',
url: apiUrl + '/Feedback/Add',
contentType: 'application/json; charset=utf-8',
data: ko.toJSON(feedBackData)
}).done(function (data) {
self.result("Done!");
}).fail(showError);
If the controller method is in an API controller it should accept JSON and the model binder will extract the values: 如果控制器方法在API控制器中,它应该接受JSON,模型绑定器将提取值:
public void Add(String screenShot, String versionId, String text)
{
String imgId = null;
if(!String.IsNullOrEmpty(screenShot))
{
Byte[] data = Convert.FromBase64String(screenShot);
// rest of code omitted
Sorry have not been able to test this for syntax etc. but should put you on the right track. 抱歉,无法对此语法等进行测试,但应该让您走上正轨。
A good tip for debugging Knockout pages is to use this line while developing so you can see what is happening in your viewModel: 调试Knockout页面的一个好方法是在开发时使用这一行,这样你就可以看到viewModel中发生了什么:
<pre data-bind="text: ko.toJSON($data, null, 2)"></pre>
See http://www.knockmeout.net/2013/06/knockout-debugging-strategies-plugin.html for more help. 有关更多帮助,请参见http://www.knockmeout.net/2013/06/knockout-debugging-strategies-plugin.html 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.