C# 如何在mvc中将图像转换为字符串并上传到firebase实时数据库
我想将我的项目的图片上传到firebase,我尝试了几天,但仍然无法将图像转换为字符串并上传。我的代码中有一些错误,请帮助我查看问题。非常感谢。 以下是我的示例代码: 学生代码C# 如何在mvc中将图像转换为字符串并上传到firebase实时数据库,c#,asp.net-mvc,firebase-realtime-database,C#,Asp.net Mvc,Firebase Realtime Database,我想将我的项目的图片上传到firebase,我尝试了几天,但仍然无法将图像转换为字符串并上传。我的代码中有一些错误,请帮助我查看问题。非常感谢。 以下是我的示例代码: 学生代码 public class Student { public string SID { get; set; } public string SName { get; set; } public string imagestrings { get; set; }
public class Student
{
public string SID { get; set; }
public string SName { get; set; }
public string imagestrings { get; set; }
}
在insert.cshtml中
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>student</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.SID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.SID, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.SID, "", new { @class = "text-danger" })
</div>
</div>
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
学生
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.SID,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.SID,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.SID,“,new{@class=“text danger”})
。
.
形象
提交
}
}
内部控制器
namespace school.Controllers
{
public class StudentController : Controller
{
IFirebaseConfig config = new FirebaseConfig
{
AuthSecret = "myfirebase",//ignore this
BasePath = "myfirebase",
};
IFirebaseClient client;
public ActionResult StudentList()
{
client = new FireSharp.FirebaseClient(config);
FirebaseResponse response = client.Get("Students");
dynamic data = JsonConvert.DeserializeObject<dynamic>(response.Body);
var list = new List<Student>();
foreach(var item in data)
{
list.Add(JsonConvert.DeserializeObject<Student>(((JProperty)item).Value.ToString()));
}
return View(list);
}
[HttpGet]
public ActionResult Insert()
{
return View();
}
[HttpPost]
public ActionResult Insert(Student student)
{
try
{
AddItemToFirebase(student);
ModelState.AddModelError(string.Empty, "Add sucess");
}
catch (Exception ex)
{
ModelState.AddModelError(string.Empty, ex.Message);
}
return View();
}
//problem is here
public ActionResult Create(string imagestring)
{
byte [] url = Convert.FromBase64String(imagestring);
MemoryStream ms = new MemoryStream(url);
return Item.FromStream(url);
}
private void AddStudentToFirebase(Student student)
{
client = new FireSharp.FirebaseClient(config);
var data = student;
PushResponse response = client.Push("Students/", data);
data.SID = response.Result.name;
SetResponse setResponse = client.Set("Students/"+data.SID, data);
}
}
}
namespace-school.controller
{
公共类学生控制器:控制器
{
IFirebaseConfig config=新FirebaseConfig
{
AuthSecret=“myfirebase”,//忽略此
BasePath=“myfirebase”,
};
如果是第二客户;
公共行动结果学生名单()
{
client=new FireSharp.FirebaseClient(配置);
FirebaseResponse=client.Get(“学生”);
dynamic data=JsonConvert.反序列化对象(响应.Body);
var list=新列表();
foreach(数据中的var项)
{
Add(JsonConvert.DeserializeObject(((JProperty)项).Value.ToString());
}
返回视图(列表);
}
[HttpGet]
公共行动结果插入()
{
返回视图();
}
[HttpPost]
公共行动结果插入(学生)
{
尝试
{
AddItemToFirebase(学生);
AddModelError(string.Empty,“添加成功”);
}
捕获(例外情况除外)
{
AddModelError(string.Empty,ex.Message);
}
返回视图();
}
//问题就在这里
公共操作结果创建(字符串imagestring)
{
字节[]url=Convert.FromBase64String(imagestring);
MemoryStream ms=新的MemoryStream(url);
返回Item.FromStream(url);
}
私有void AddStudentToFirebase(学生)
{
client=new FireSharp.FirebaseClient(配置);
var数据=学生;
PushResponse=client.Push(“学生/”,数据);
data.SID=response.Result.name;
SetResponse SetResponse=client.Set(“Students/”+data.SID,data);
}
}
}
问题是来自控制器,但我不知道如何解决它,转换图像的逻辑我不太清楚
namespace school.Controllers
{
public class StudentController : Controller
{
IFirebaseConfig config = new FirebaseConfig
{
AuthSecret = "myfirebase",//ignore this
BasePath = "myfirebase",
};
IFirebaseClient client;
public ActionResult StudentList()
{
client = new FireSharp.FirebaseClient(config);
FirebaseResponse response = client.Get("Students");
dynamic data = JsonConvert.DeserializeObject<dynamic>(response.Body);
var list = new List<Student>();
foreach(var item in data)
{
list.Add(JsonConvert.DeserializeObject<Student>(((JProperty)item).Value.ToString()));
}
return View(list);
}
[HttpGet]
public ActionResult Insert()
{
return View();
}
[HttpPost]
public ActionResult Insert(Student student)
{
try
{
AddItemToFirebase(student);
ModelState.AddModelError(string.Empty, "Add sucess");
}
catch (Exception ex)
{
ModelState.AddModelError(string.Empty, ex.Message);
}
return View();
}
//problem is here
public ActionResult Create(string imagestring)
{
byte [] url = Convert.FromBase64String(imagestring);
MemoryStream ms = new MemoryStream(url);
return Item.FromStream(url);
}
private void AddStudentToFirebase(Student student)
{
client = new FireSharp.FirebaseClient(config);
var data = student;
PushResponse response = client.Push("Students/", data);
data.SID = response.Result.name;
SetResponse setResponse = client.Set("Students/"+data.SID, data);
}
}
}