如何使用ASP.NET web api和knockout将图像上传到数据库中 [英] How to upload image into database with ASP.NET web api and knockout

查看:64
本文介绍了如何使用ASP.NET web api和knockout将图像上传到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为下面提到的代码创建一个可绑定的视图,我是新的淘汰赛js,我找不到任何关于使用web api和淘汰js上传图像到数据库的例子



我尝试过:



公共类代理

{

public int Id {get;组; } $ / $
公共字符串FirstName {get;组; }

公共字符串LastName {get;组; } $ / $
public string ImagePath {get;组; }

}



我的后api控制器



public async任务< HttpResponseMessage>发布(代理newAgent)

{

if(newAgent!= null)

{

string regEx = data:(。+); base64,(。+);

匹配匹配= Regex.Match(newAgent.ImagePath,regEx);

if(match.Success) )

{

string imageType = match.Groups [1] .Value;

string base64image = match.Groups [2] .Value;



if(imageType!= null&& base64image!= null)

{

string imageRegEx =image /(.+);

match = Regex.Match(imageType,imageRegEx);

if(match.Success)

{

string fileExtension = match.Groups [1] .Value;

byte [] image = Convert.FromBase64String(base64image);



string path = HttpContext.Current.Server.MapPath(〜/ images);

string fileName = newAgent.FirstName + newAgent.LastName;





string targetFile = fileName +。 + fileExtension;

int index = 0;

while(File.Exists(Path.Combine(path,targetFile)))

{

index ++;

targetFile = fileName + index +。 + fileExtension;

}



File.WriteAllBytes(Path.Combine(path,targetFile),image);

newAgent.ImagePath =images /; + targetFile;



newAgent = Database.AddAgent(newAgent);



var response = Request.CreateResponse (HttpStatusCode.Created,newAgent);

string uri = Url.Link(GetAgent,new {id = newAgent.Id});

response.Headers.Location =新的Uri(uri);



返回回复;

}

}

}

}

抛出新的HttpResponseException(Request.CreateErrorResponse(

HttpStatusCode.BadRequest,无法反序列化代理));



}

how do i create a bindable view for the below mentioned code, am new into knockout js, and i couldn't find any example regarding uploading images into database using web api and knockout js

What I have tried:

public class Agent
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string ImagePath { get; set; }
}

my post api controller

public async Task<HttpResponseMessage> Post(Agent newAgent)
{
if (newAgent != null)
{
string regEx = "data:(.+);base64,(.+)";
Match match = Regex.Match(newAgent.ImagePath, regEx);
if (match.Success)
{
string imageType = match.Groups[1].Value;
string base64image = match.Groups[2].Value;

if (imageType != null && base64image != null)
{
string imageRegEx ="image/(.+)";
match = Regex.Match(imageType, imageRegEx);
if (match.Success)
{
string fileExtension = match.Groups[1].Value;
byte[] image = Convert.FromBase64String(base64image);

string path = HttpContext.Current.Server.MapPath("~/images");
string fileName = newAgent.FirstName + newAgent.LastName;


string targetFile = fileName + "." + fileExtension;
int index = 0;
while (File.Exists(Path.Combine(path, targetFile)))
{
index++;
targetFile = fileName + index + "." + fileExtension;
}

File.WriteAllBytes(Path.Combine(path, targetFile), image);
newAgent.ImagePath = "images/"; + targetFile;

newAgent = Database.AddAgent(newAgent);

var response = Request.CreateResponse(HttpStatusCode.Created, newAgent);
string uri = Url.Link("GetAgent", new { id = newAgent.Id });
response.Headers.Location = new Uri(uri);

return response;
}
}
}
}
throw new HttpResponseException(Request.CreateErrorResponse(
HttpStatusCode.BadRequest, "Could not deserialize agent"));

}

推荐答案





以下是我认为可以帮助您解决此问题的链接(上传ASP.NET MVC中使用JavaScript和C#的文件。 [ ^ ]),它使用ajax将文件返回给服务器然后从那里你可以拿到文件并插入你的数据库。您还没有说过您使用什么类型的技术进行数据库连接(ole,odbc,entity),所以我无法就如何将其插入数据库给出任何建议。您还没有说明您使用的是哪种类型的数据库(MS SQL,MY SQL,Access DB,MongoDB等)。我希望这会有所帮助!!
Hi,

Here is a link I think will help you with this issue(Upload files in ASP.NET MVC with JavaScript and C#.[^]), it uses ajax to return the file to the server then from there you can take the file and insert into your DB. You have not said what type of technology you are using for you DB connection (ole, odbc, entity) so I can't give you any advice on how to insert this into your DB. You also have not said what type of DB you are using(MS SQL, MY SQL, Access DB, MongoDB etc.). I hope this will help!!


这篇关于如何使用ASP.NET web api和knockout将图像上传到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆