无法在给定路径中找到文件

时间:2017-04-08 16:21:30

标签: c# asp.net-mvc

我正在研究我的学校项目,我正在尝试插入数据并将图像文件上传到数据库,但是我收到了这个错误。

Here is the screenshot

这是代码: 我无法理解错误意味着什么。

public static void AddItems(StoreItems i)
{

    byte[] bytes;
    if (string.IsNullOrEmpty(i.PicturePath))
    {
        string filename = System.Web.HttpContext.Current.Server.MapPath("~/Content/Images/default-artwork.png");
        bytes = System.IO.File.ReadAllBytes(filename);
    }

    else
    {
        string filename = i.PicturePath;
        bytes = System.IO.File.ReadAllBytes(filename);


    }


    SqlConnection con = new SqlConnection(DAL.cs);
    con.Open();
    SqlCommand com = new SqlCommand(
        "INSERT INTO AlbumsTb ( AlbumName, Artist, Genre, DateReleased, Price, Downloads, Listens, RecordLabel, DateAdded, AlbumArt) VALUES( @AlbumName, @Artist, @Genre, @DateReleased, @Price, @Downloads, @Listens, @RecordLabel, @DateAdded, @AlbumArt)", con);
    //com.Parameters.AddWithValue("@ID", i.ID);
    com.Parameters.AddWithValue("@AlbumName", SqlDbType.VarChar).Value = i.AlbumName;
    com.Parameters.AddWithValue("@Artist", SqlDbType.VarChar).Value = i.Artist;
    com.Parameters.AddWithValue("@Genre", SqlDbType.VarChar).Value = i.Genre;
    com.Parameters.AddWithValue("@DateReleased", SqlDbType.Date).Value = i.DateReleased;
    com.Parameters.AddWithValue("@Price",i.Price);
    com.Parameters.AddWithValue("@Downloads", i.Downloads);
    com.Parameters.AddWithValue("@Listens", i.Listens);
    com.Parameters.AddWithValue("@RecordLabel", SqlDbType.VarChar).Value = i.RecordLabel;
    com.Parameters.AddWithValue(@"DateAdded", DateTime.Now.ToString());

    com.Parameters.AddWithValue("@AlbumArt", SqlDbType.VarBinary).Value = bytes;

    com.ExecuteNonQuery();
    con.Close();
}

3 个答案:

答案 0 :(得分:1)

根据您上一个问题中的代码,您没有将文件保存在调用方法的任何位置。因此,即使对象具有图片路径,也不会使用该名称保存任何内容。因此没有什么可读的。

错误消息指出它无法在提供的路径中找到文件。这是真的,因为你没有在那个地方保存任何东西。

[HttpPost]
public ActionResult AddItems(FormCollection form)
{
    //...other code removed for brevity

    byte[] bytes;
    var files = Request.Files;
    if(files.Count > 0) {
        var stream = files[0].InputStream;
        //Get image data from stream and stored in bytes
        using(var memoryStream = new MemoryStream()) {
            stream.CopyTo(memoryStream);
            bytes = memoryStream.ToArray();
        }
    } else {
        //...Set default image data.
        var filename = this.HttpContext.Server.MapPath("~/Content/Images/default-artwork.png");
        bytes = System.IO.File.ReadAllBytes(filename);
    }

    //Assuming a property to hold the image data byte[] AlbumArt { get; set; }
    i.AlbumArt = bytes;

    DAL.AddItems(i);
    //...other code removed for brevity
}

重构DAL以将字节作为输入参数或将其作为输入类的byte []属性。

public static void AddItems(StoreItems i) { 
    using(var con = new SqlConnection(DAL.cs) {
        con.Open();
        var sql = "INSERT INTO AlbumsTb ( AlbumName, Artist, Genre, DateReleased, Price, Downloads, Listens, RecordLabel, DateAdded, AlbumArt) VALUES( @AlbumName, @Artist, @Genre, @DateReleased, @Price, @Downloads, @Listens, @RecordLabel, @DateAdded, @AlbumArt)";
        using(var com = new SqlCommand(sql, con) {
            com.Parameters.AddWithValue("@AlbumName", SqlDbType.VarChar).Value = i.AlbumName;
            com.Parameters.AddWithValue("@Artist", SqlDbType.VarChar).Value = i.Artist;
            com.Parameters.AddWithValue("@Genre", SqlDbType.VarChar).Value = i.Genre;
            com.Parameters.AddWithValue("@DateReleased", SqlDbType.Date).Value = i.DateReleased;
            com.Parameters.AddWithValue("@Price",i.Price);
            com.Parameters.AddWithValue("@Downloads", i.Downloads);
            com.Parameters.AddWithValue("@Listens", i.Listens);
            com.Parameters.AddWithValue("@RecordLabel", SqlDbType.VarChar).Value = i.RecordLabel;
            com.Parameters.AddWithValue("@DateAdded", DateTime.Now.ToString());

            com.Parameters.AddWithValue("@AlbumArt", SqlDbType.VarBinary).Value = i.AlbumArt;

            com.ExecuteNonQuery();
        }
    }
}

答案 1 :(得分:0)

在IIS Express上执行应用程序时,您的输入参数指向“C:\ Program Files(x86)\ IIS Express \”位置。该路径存储在StoreItems.PicturePath中是错误的。

调试您正在设置此路径的代码。

答案 2 :(得分:0)

很明显,您正试图在错误的文件夹中访问该文件,该消息毫无疑问。我会做这样的事情:

string filename = i.PicturePath;
if (string.IsNullOrEmpty(filename))
{
    filename = "default-artwork.png";
}
string fullPath = System.Web.HttpContext.Current.Server.MapPath("~/Content/Images/" + filename);
byte[] bytes = System.IO.File.ReadAllBytes(fullPath);