如何在MVC中

问题描述

我想将我的项目的图片上传到firebase,我尝试了几天,但仍然无法将图片转换为字符串并上传。我的代码中出现错误,请帮助我查看问题。谢谢。 这是我的示例代码: Student.cs代码

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>

。 。

            <div class="form-group">
                    Image
                    <div class="col-md-10">
                        <input id="image" type="image" class="form-control" name="image" accept="image/*" required/>
                    </div>
                </div>

            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <button type="submit" class="btn btn-primary" >Submit</button>
                </div>
            </div>
        </div>
    }
}

在控制器中

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

问题出在控制器上,但我不知道该如何解决,转换图像的逻辑还不清楚。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)