使用挖空和 webapi c# 上传图像

Image upload with Knockout and webapi c#

本文关键字:图像 webapi      更新时间:2023-09-26

我正在尝试使用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(); 
    }

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

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

更改表单 HTML:

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

更改视图模型代码,如下所示:

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

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

调试挖空页的一个很好的提示是在开发时使用此行,以便您可以看到视图中发生的情况模型:

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

有关更多帮助,请参阅 http://www.knockmeout.net/2013/06/knockout-debugging-strategies-plugin.html。