【问题标题】:Image upload with Knockout and webapi c#使用 Knockout 和 webapi c# 上传图像
【发布时间】:2016-04-20 22:55:24
【问题描述】:

我正在尝试使用 Knokout JS 和 web api 上传图像。这是我的代码

<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>

我正在使用这个自定义绑定

https://github.com/TooManyBees/knockoutjs-file-binding

然后在我的脚本代码中我正在这样做

    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);

}

我不确定代码的服务器部分。我已经写到这里了

    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(); 

    }

关于如何做到这一点的任何想法?

【问题讨论】:

  • 您需要使用ko.toJSON() 而不是JSON.stringify - observables 是函数,不会被返回。见knockmeout.net/2011/04/utility-functions-in-knockoutjs.html
  • 我注意到的第二件事是您的上传库(敲除文件绑定)将文件数据存储在可观察的fileInput 中,但随后您在控制器中使用HttpPostedFileBase - 您正在混合不同的方法。由于发布的脚本只是部分内容,很难添加更多内容
  • 是的,我解决了这个问题,但它仍然无法正常工作。我在 base64 编码字符串中获取 fileInput()。也许这就是问题所在。也许我需要使用其他技术上传它。
  • 看看我的一篇博文,它使用了不同的方法:conficient.wordpress.com/2013/07/22/…
  • 已添加答案作为建议

标签: javascript c# asp.net-web-api knockout.js


【解决方案1】:

fileInput 包含 base64 编码的文件数据。它是一个字符串,所以 HttpPostedFileBase 不起作用。

更改表单 HTML:

  <input class="form-control" type="file"
               data-bind="file: {data: fileInput}" />

修改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);

如果控制器方法在 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

抱歉,无法测试此语法等,但应该让您走上正确的轨道。

调试 Knockout 页面的一个好技巧是在开发时使用此行,这样您就可以看到 viewModel 中发生了什么:

<pre data-bind="text: ko.toJSON($data, null, 2)"></pre>

请参阅http://www.knockmeout.net/2013/06/knockout-debugging-strategies-plugin.html 以获得更多帮助。

【讨论】:

  • 最好的部分是调试技巧
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-10
  • 1970-01-01
  • 1970-01-01
  • 2016-07-31
  • 2013-01-08
  • 2013-01-19
  • 2018-12-17
相关资源
最近更新 更多