c# - Is there any way to save an image file to Mongo DB using ASP.NET MVC?
问题描述
i've been trying to create a program that saves user's input into Mongo DB, but when it comes to user's file, i am struggling, i have done a little research about how to do it using GridFs, can I use GridFS in C#? or do i have to find other ways to save the file to mongo db?
edit: here is my controller
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MongoDB.Bson;
using MongoDB.Driver;
namespace officialaspnet.Controllers
{
public class vaccinationController : Controller
{
// GET: vaccination
public ActionResult Index()
{
return View();
}
public ActionResult Getuserdetail()
{
return View();
}
[HttpPost]
public ActionResult Saveuserdetail(string firstname, string lastname, string dob, string studentid, string gender, string email, string phonenumber, string address, string city, string zipcode, string state, HttpPostedFileBase file)
{
var client = new MongoClient("mongodb://localhost:27017");
var database = client.GetDatabase("test1");
var collec = database.GetCollection<BsonDocument>("test");
var document = new BsonDocument
{
{"Firstname", firstname },
{"Lastname", lastname },
{"Date of birth", dob },
{"Student ID", studentid },
{"Gender", gender },
{"Email", email },
{"Phone number", phonenumber },
{"Address", address },
{"City", city },
{"Zipcode", zipcode },
{"State", state },
};
collec.InsertOneAsync(document);
return Content("Account created");
}
}
}
here is my form:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<!--providing css-->
<style>
body {
margin: 0;
padding: 0;
background: url() no-repeat;
background-size: cover;
font-family: sans-serif;
line-height: 2.6;
}
.topnav {
overflow: hidden;
background-color: whitesmoke;
}
.topnav a {
float: left;
color: black;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: white;
color: black;
}
.topnav a.active {
background-color: black;
color: white;
}
.userinput-box {
width: 100%;
padding: 10px 0;
margin: 5px 0;
border-left: 0;
border-top: 0;
border-right: 0;
border-bottom: 1px;
outline: none;
background: transparent;
line-height: 2.6;
}
.userinput-group {
top: 180px;
position: absolute;
width: 280px;
line-height: 2.6;
}
.button {
background-color: #cbcbcb;
}
</style>
</head>
<body>
<script src=""></script>
<!--top nav-->
<div class="topnav">
<a class="active" href="">Log in</a>
<a href="https://exampletonyhuang.azurewebsites.net/">Home</a>
<a href="">News</a>
<a href="">Contact</a>
<a href="">About</a>
<a href="">Shopping Cart</a>
<a href="">Billing info</a>
</div>
<div class="register-box">
<center><h1><b>Covid Vaccine Verification form</b></h1></center>
<!--code for form-->
<form class="userinput-box" method="post" action="Saveuserdetail">
<center>
<!--giving 3 input field and defining their id-->
<h3>First name: <input type="text" name="firstname" id="firstname" required> Last name:<input type="text" name="lastname" id="lastname" required></h3>
<h3>Date of birth: <input type="date" name="dob" id="dob" required>Student ID: <input type="number" name="studentid" id="studenid" required></h3>
<h3>Gender: <input type="radio" id="male" name="gender" value="male"><label for="male">Male </label><input type="radio" id="female" name="gender" value="female"><label for="female">Female </label><input type="radio" id="other" name="gender" value="other"><label for="other">Other: </label><input type="text" name="othergender" id="othergender" /></h3>
<h3>Email: <input type="text" name="email" id="email" required> Phone#: <input type="number" name="phonenumber" id="phonehumber" required></h3>
<h3>Address: <input type="text" name="address" id="address" size="65" required></h3>
<h3>City: <input type="text" name="city" id="city" required> Zip code: <input type="number" name="zipcode" id="zipcode"> State: <input type="text" name="state" id="state" required></h3>
<h3>Add an attachment <input type="file" id="file" name="file" required ></h3>
<!--button for submitting-->
<br /><button type="submit" value="Save" class="button"><h3>Submit</h3></button>
</center>
</form>
</div>
</body>
</html>
解决方案
推荐阅读
- c - 在 C 中创建具有邻接矩阵的图
- python-3.x - __new__、__init__ 和元类(和超类)
- javascript - 可以通过 php 或 js 获取同一无线路由器的 SSID/网络名称吗?
- grid - 容器的缩放比例不小于 576px
- python-3.x - 有什么方法可以在 Python 中以字节为单位将图像添加到 pdf 中?
- elixir - 具有动态构建条件的 Ecto 连接
- r - 如何按每日最小值聚合数据框
- cqrs - 拉戈姆 | 读取端处理器的返回值
- amazon-web-services - 如何将步骤中的输入插入到 json 结构并传递到步骤函数中的下一步?
- javascript - 向后打印特定索引的值