错误:当前上下文中不存在名称“名称” [英] Error: The name 'Name' does not exist in the current context

查看:231
本文介绍了错误:当前上下文中不存在名称“名称”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经去编辑代码 - >这绝不是一件好事!

I've gone editing code -> which is never a good thing!

代码用于MVC中的文件上传器。问题是我一次上传两个文件,这意味着它们被插入到数据库中的不同行中。这是原始代码:

the code is for a file uploader in MVC. The thing was I was uploading two files at a time which meant they were inserted into separate rows in a database. This is the original code:

    public ActionResult Index()
    {
        ViewData["Message"] = "Convert your eBooks!";

        foreach (string upload in Request.Files)
        {
            if (!Request.Files[upload].HasFile1()) continue;

            string mimeType = Request.Files[upload].ContentType;
            Stream fileStream = Request.Files[upload].InputStream;
            string fileName = Path.GetFileName(Request.Files[upload].FileName);
            int fileLength = Request.Files[upload].ContentLength;
            byte[] fileData = new byte[fileLength];
            fileStream.Read(fileData, 0, fileLength);

            const string connect = @"Server=localhost;Database=Images;user id=taraw; password=siemensbs;";
            using (var conn = new SqlConnection(connect))
            {
                var qry = "INSERT INTO FileStore (FileContent, MimeType, FileName) VALUES (@FileContent, @MimeType, @FileName)";
                var cmd = new SqlCommand(qry, conn);
                cmd.Parameters.AddWithValue("@FileContent", fileData);
                cmd.Parameters.AddWithValue("@MimeType", mimeType);
                cmd.Parameters.AddWithValue("@FileName", fileName);
                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }

        return View();
    }

这是我尝试修改代码以便单独接收文件而不是使用循环,并将它们插入数据库表中的单个行:

Here is my attempt at modifying the code in order to take in the files separately as opposed to using a loop, and inserting them into a single row in a database table:

    public ActionResult Index()
    {

        if (!Request.Files["FileUpload1"].HasFile1())
        {
            string mimeTypePDF = Request.Files["FileUpload1"].ContentType;
            Stream fileStreamPDF = Request.Files["FileUpload1"].InputStream;
            string fileNamePDF = Path.GetFileName(Request.Files["FileUpload1"].FileName);
            int fileLengthPDF = Request.Files["FileUpload1"].ContentLength;
            byte[] fileDataPDF = new byte[fileLengthPDF];
            fileStreamPDF.Read(fileDataPDF, 0, fileLengthPDF);
        }

            if(!Request.Files["FileUpload2"].HasFile1())
        {
                string mimeTypeCover = Request.Files["FileUpload2"].ContentType;
                Stream fileStreamCover = Request.Files["FileUpload2"].InputStream;
                string fileNameCover = Path.GetFileName(Request.Files["FileUpload2"].FileName);
                int fileLengthCover = Request.Files["FileUpload2"].ContentLength;
                byte[] fileDataCover = new byte[fileLengthCover];
                fileStreamCover.Read(fileDataCover, 0, fileLengthCover);
        }

            const string connect = @"Server=localhost;Database=Images;user id=taraw; password=siemensbs;";
            using (var conn = new SqlConnection(connect))
            {
                var qry = "INSERT INTO Book (FileContentPDF, MimeTypePDF, FileNamePDF, FileContentCover, MimeTypeCover, FileNameCover) VALUES (@FileContentPDF, @MimeTypePDF, @FileNamePDF, @FileContentCover, @MimeTypeCover, @FileNameCover)";
                var cmd = new SqlCommand(qry, conn);
                cmd.Parameters.AddWithValue("@FileContentPDF", fileDataPDF);
                cmd.Parameters.AddWithValue("@MimeTypePDF", mimeTypePDF);
                cmd.Parameters.AddWithValue("@FileNamePDF", fileNamePDF);
                    cmd.Parameters.AddWithValue("@FileContentCover", fileDataCover);
                cmd.Parameters.AddWithValue("@MimeTypeCover", mimeTypeCover);
                cmd.Parameters.AddWithValue("@FileNameCover", fileNameCover);           
                conn.Open();
                cmd.ExecuteNonQuery();
            }

        return View();
     }

现在我为每个cmd.Parameters.AddWithValue收到以下错误:

Now I get the following errors for each cmd.Parameters.AddWithValue:


当前上下文中名称'fileDataPDF'不存在

The name 'fileDataPDF' does not exist in the current context

我认为这是因为它不在IF语句之内,但我对如何构造它有点困惑。我最终想用linq将文件插入到数据库中,因为上面的方法并不理想,但我现在的主要目的是让这一点工作。
任何帮助都将受到极大的赞赏:)

I'm presuming this is because it is outside of the IF statements but I'm a bit stuck on how to structure it. I eventually want to use linq to insert the files into the database as the above method is not ideal, but my main aim for now is to just get this bit working. Any help would be appreciated greatly :)

推荐答案

您需要在if条件范围之外声明变量。例如。

You need to declare the variables outside of the if conditional scopes. e.g.

    string mimeTypePDF;
    string fileNamePDF;
    byte[] fileDataPDF;

    if (!Request.Files["FileUpload1"].HasFile1())
    {
        mimeTypePDF = Request.Files["FileUpload1"].ContentType;
        Stream fileStreamPDF = Request.Files["FileUpload1"].InputStream;
        fileNamePDF = Path.GetFileName(Request.Files["FileUpload1"].FileName);
        int fileLengthPDF = Request.Files["FileUpload1"].ContentLength;
        fileDataPDF = new byte[fileLengthPDF];
        fileStreamPDF.Read(fileDataPDF, 0, fileLengthPDF);
    }

(Fwiw,我认为有更好的方法来处理这个多文件上传,但是以上是处理问题的最简单方法。)

(Fwiw, I think there are better ways to handle this multifile upload, but the above is simplest way to handle your question.)

这篇关于错误:当前上下文中不存在名称“名称”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆