微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有没有办法使用 ASP.NET MVC 将图像文件保存到 Mongo DB?

如何解决有没有办法使用 ASP.NET MVC 将图像文件保存到 Mongo DB?

我一直在尝试创建一个程序,将用户的输入保存到 Mongo DB 中,但是当涉及到用户文件时,我很挣扎,我已经对如何使用 GridFs 进行了一些研究,我可以使用 GridFS在 C# 中?还是我必须找到其他方法文件保存到 mongo db?

编辑: 这是我的控制器

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

这是我的表格:

@{
    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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。