Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将图像拖放到数据库中_C#_Asp.net - Fatal编程技术网

C# 将图像拖放到数据库中

C# 将图像拖放到数据库中,c#,asp.net,C#,Asp.net,我使用的是拖放区,如果它保存到服务器上的文件中,效果非常好,但是我想保存到数据库中,我无法从代码中获取图像元素 public void ProcessRequest(HttpContext context) { context.Response.ContentType = "image/png"; string dirFullPath = HttpContext.Current.Server.MapPath("~/MediaUploader/"); string[] f

我使用的是拖放区,如果它保存到服务器上的文件中,效果非常好,但是我想保存到数据库中,我无法从代码中获取图像元素

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "image/png";

    string dirFullPath = HttpContext.Current.Server.MapPath("~/MediaUploader/");
    string[] files;
    int numFiles;

    files = System.IO.Directory.GetFiles(dirFullPath);
    numFiles = files.Length;
    numFiles = numFiles + 1;

    string str_image = "";

    foreach (string s in context.Request.Files)
    {
        HttpPostedFile file = context.Request.Files[s];
        string fileName = file.FileName;
        string fileExtension = file.ContentType;

        if (!string.IsNullOrEmpty(fileName))
        {
            fileExtension = Path.GetExtension(fileName);
            str_image = "MyPHOTO_" + numFiles.ToString() + fileExtension;
            string pathToSave = HttpContext.Current.Server.MapPath("~/MediaUploader/") + str_image;
            file.SaveAs(pathToSave);
        }
    }

    context.Response.Write(str_image);
}

通过在字节数组中调用file.InputStream.Read,可以从file.InputStream获取组成文件的字节。一旦有了字节,就可以将它们写入二进制类型的数据库列

我喜欢使用base64字符串而不是二进制字符串,因为它们更容易用于各种I/O函数。因此,我下面的示例假设您正在使用该方法。这绝不是生产代码,而是一个包含必要功能的示例

    public class DatabaseFile
    {

        /// <summary>
        /// The file name, the database column to which it corresponds is a nvarchar(256)
        /// </summary>
        public string Name = string.Empty;

        /// <summary>
        /// The Mime type of the file, the database column to which it corresponds is a nvarchar(64)
        /// </summary>
        public string Mime = string.Empty;

        /// <summary>
        /// The file data as a base64 string, the database column to which it corresponds is a ntext
        /// </summary>
        public string Data = string.Empty;

        /// <summary>
        /// The file data as a byte array
        /// </summary>
        public byte[] BinaryData
        {
            get { return Convert.FromBase64String(Data); }
            set { Data = Convert.ToBase64String(value); }
        }

        /// <summary>
        /// Constructor to create a DatabaseFile from a HttpPostedFile
        /// </summary>
        /// <param name="file"></param>
        public DatabaseFile(HttpPostedFile file)
        {
            Name = file.FileName;
            Mime = file.ContentType;
            byte[] fileBytes = new byte[file.ContentLength];
            file.InputStream.Read(fileBytes, 0, file.ContentLength);
            BinaryData = fileBytes;
        }

        /// <summary>
        /// Save the file information and data to a database table called [FileTable].
        /// </summary>
        /// <param name="sqlConnection"></param>
        public void SaveToDatabase(SqlConnection sqlConnection)
        {

            // Parameterized Insert command
            SqlCommand sqlCommand = new SqlCommand("insert [FileTable] ([Name], [Mime], [Data]) values (@Name, @Mime, @Data)", sqlConnection);

            // Create the necessary parameters
            sqlCommand.Parameters.Add("@Name", SqlDbType.NVarChar, 256);
            sqlCommand.Parameters.Add("@Mime", SqlDbType.NVarChar, 64);
            sqlCommand.Parameters.Add("@Data", SqlDbType.NText);

            // Assign the parameters
            sqlCommand.Parameters["@Name"].Value = Name;
            sqlCommand.Parameters["@Mime"].Value = Mime;
            sqlCommand.Parameters["@Data"].Value = Data;

            // Execute the command
            try
            {
                sqlConnection.Open();
                sqlCommand.ExecuteNonQuery();
            }
            finally
            {
                sqlConnection.Close();
            }

        }
    }

我在这段代码中没有看到任何与数据库有关的内容。什么意思你不能从后面的代码中获取图像?张贴在这里的代码不起作用吗?我很困惑,因为您说过保存到文件是可行的,但您看到的问题与数据库有关。你可以发布不起作用的代码吗?im使用drop zone生成图像,在上传后作为HTML标记img预览。它可以保存为一个文件,我不想像上面写的那样,我试图保存在数据库中,我不能访问它作为正常的asp图像它!有没有办法将图像从处理程序保存到数据库?为什么numFiles不是简单的files.Length+1?为什么必须分两步进行?另外,考虑你的方法的副作用。如果您将其作为参数传递会更好。这是一个很好的反射,否则,将来会出现一些令人讨厌的bug,在运行时会对整个系统产生副作用,而您不知道,无论何时使用给定的一组参数显式调用代码,对于输入中的相同值,这些参数将始终为您提供相同的结果。您能举个例子吗_
DatabaseFile dbFile = new DatabaseFile(context.Request.Files[s]);
dbFile.SaveToDatabase(sqlConnection);