使用挖空和 webapi c# 上传图像
Image upload with Knockout and webapi c#
我正在尝试使用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。
相关文章:
- 如何使用url加载程序在webpack中导入多个图像
- nodejs-expressjs上传图像并显示它们
- 使用JSP从服务器检索和显示图像
- 角度:在ng重复上切换图像
- 画廊图像未显示
- Phonegap-(安卓/iphone)多个图像的图像库出现问题
- Javascript排序的图像弹出窗口..可以't单独弹出
- 画布数据到图像
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 使图像在单击时展开到不大于浏览器
- Html页面上的多个Base64图像和平滑加载
- UIAutomation放大图像
- 使用javascript在Flash中加载外部图像
- 可以在这里为背景图像设置滤镜吗
- 当图像转换为本地存储的DataURL时,EXIF被删除
- 使用 js 将背景图像设置为 HTML
- 使用挖空和 webapi c# 上传图像
- 在json中传递base64字节的图像数组给webApi
- 无法从Canvas上的WebApi调用绘制图像
- 将JSON发送到WebAPI - JSON对象包含一个图像(字节)-得到错误:表达式太长或太复杂而无法编译