【问题标题】:How to upload image to a database using webmatrix 3?如何使用 webmatrix 3 将图像上传到数据库?
【发布时间】:2015-05-14 08:57:27
【问题描述】:

我有两个不同的 cshtml 页面。一个用于创建数据库,第二个用于显示数据。不知道怎么上传图片。每次我上传 png 文件时,它都会显示“4:无法将 'System.String' 类型的对象转换为 'System.Byte[]' 类型。” 数据库中的数据类型是image

代码如下:

@{
var ProductName="";
var ProductPrice="";
var ProductExpireDate="";
var ProductDescription="";
var ProductImage="";
  ProductName=Request["PName"];
  ProductPrice=Request["Price"];
  ProductExpireDate= Request["ExpireDate"];
  ProductDescription= Request["Description"];
  ProductImage = Request["Image"];
if(IsPost){
var SQLINSERT = "INSERT INTO ProductInfo (Name, Price, ExpireDate, Description, Image) VALUES (@0,@1,@2,@3,@4)";
var db = Database.Open("Product");
db.Execute(SQLINSERT, ProductName , ProductPrice, ProductExpireDate, ProductDescription, ProductImage);
//Response.Redirect("default.cshtml");
}
}

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8"/>
        <title> MaxProducts </title>
    </head>

  <body>
        <div id="addProducts">
            <h2>  Product Information </h2>
                <form action="" method="post" name="ProductsForm" >
                  Name:<br>
                  <input type="text" name="PName"><br>
                  Price:<br>
                  <input type="text" name="Price"><br>
                  Expire Date:<br>
                  <input type="date" name="ExpireDate"><br>
                  Description:<br>
                  <textarea rows="10" cols="80" name="Description">                         </textarea><br>     
                  Image:<br>
                  <input type="file" name="Image"><br><br>
                  <input type="submit" value="Enter Product" name="EnterProduct"/><br>
            </form>
        </div>
    </body>
</html>

【问题讨论】:

    标签: html database image razor webmatrix


    【解决方案1】:

    不是 100% 确定正确的解决方案,但是当我将视频上传到数据库时,我将文件名设置为数据库,然后当我需要从数据库中提取它时,我使用一个变量来拉起视频。

    @foreach (var title in db.Query("Select * from vids" )){
        var toopen="Media/"+title.Title;
        <div id="item" class="playerdiv" onclick="window.open('@toopen','mywin','width:200px'); ">    
        <div style="margin: 10px">@RenderPage("thumbplayer.cshtml", new {
    title=title.Title            
      })</div>
        </div>
    

    然后,当有人单击图像时,它会在新窗口中打开它。它并不优雅,但对我有用。

    【讨论】:

      猜你喜欢
      • 2015-05-15
      • 1970-01-01
      • 2011-04-21
      • 2015-09-18
      • 2019-04-21
      • 2016-08-28
      • 2018-09-17
      • 2017-11-20
      相关资源
      最近更新 更多