该进程无法访问文件“image”,因为它正由另一个进程使用

时间:2014-05-01 22:33:25

标签: c# asp.net

我的Web应用程序是由ASP.NET构建的。它上传图像并将图像保存到数据库中,同时使用ASHX预览图像。当我在本地机器上测试它时效果很好。但是当我在服务器上托管它,并尝试上传相同的图像两次(保存为数据库中的不同条目)时,我遇到The process cannot access the file 'image' because it is being used by another process错误。因为它没有告诉我我的代码的哪一部分抛出了这个异常所以我不知道在哪里检查。有什么建议吗?顺便说一句,我在使用该文件后打电话给dispose()。提前谢谢。

这是ASHX文件:

public class ShowImage : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string PLID = "";
            string EMID = "";
            if (context.Request.QueryString["PLID"] != null)
                PLID = (context.Request.QueryString["PLID"]);
            else if (context.Request.QueryString["EMID"] != null)
                EMID = (context.Request.QueryString["EMID"]);
            else
                throw new ArgumentException("No parameter specified");

            context.Response.ContentType = "image/png";

            if (PLID != string.Empty)
                context.Response.BinaryWrite(ShowEmpImage(PLID));
            else if (EMID != string.Empty)
                context.Response.BinaryWrite(ShowEmImage(EMID));

        }

        public byte[] ShowEmpImage(string PLID)
        {
            string connectionString = WebConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;

            //string conn = ConfigurationManager.ConnectionStrings["EmployeeConnString"].ConnectionString;
            SqlConnection connection = new SqlConnection(connectionString);
            string sql = "SELECT [ChargingBoxLocationImage] FROM [Parking Lot] WHERE [ID] = @PLID";

            SqlCommand cmd = new SqlCommand(sql, connection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@PLID", PLID);
            connection.Open();
            object img = cmd.ExecuteScalar();
            try
            {
                //return new MemoryStream((byte[])img);
                return (byte[])img;
            }
            catch
            {
                return null;
            }
            finally
            {
                connection.Close();
            }
        }

以下是我预览图片的方式:

    protected void btnPreviewImage_Click(object sender, EventArgs e)
            {
                bool blnPassesImageValidation = true; // Checks that file is an image, and also that it is < 1 MB

                FileUpload img = (FileUpload)fileUploadLocationPic;
                Byte[] imgByte = null;
                if (img.HasFile && img.PostedFile != null)
                {
                    string strFileExtension = Path.GetExtension(img.PostedFile.FileName.ToString());
                    if (!chkValidExtension(strFileExtension))
                    {
                        lblFileUploadError.Text += "'" + strFileExtension + "'" + " Format is not allowed. <br>";

                        blnPassesImageValidation = false;
                    }
                    // Create a FILE
                    HttpPostedFile File = fileUploadLocationPic.PostedFile;
                    // Create byte array
                    imgByte = new Byte[File.ContentLength];
                    // force control to load data
                    File.InputStream.Read(imgByte, 0, File.ContentLength);

                    if (imgByte.Length / 1024f / 1024f > 1)
                    {
                        lblFileUploadError.Text += "Image size must be less than 1 MB.";
                        blnPassesImageValidation = false;
                    }
                    if (!blnPassesImageValidation) // If the image doesn't pass validation, stop function
                    {
                        PopUpMessage("Error while previewing.");
                        return;
                    }
                    else
                    {
                        File.SaveAs(MapPath(System.IO.Path.GetFileName(File.FileName).ToLower().ToString()));
                        imageEVModel.ImageUrl = System.IO.Path.GetFileName(File.FileName).ToLower().ToString();

                    }
                }
                else
                {
                    PopUpMessage("No Image selected!");
                    return;
                }
            }

以下是我将其保存到数据库的方法:

FileUpload img = (FileUpload)fileUploadLocationPic;
            Byte[] imgByte = null;
            bool blnByteExists = false;

            bool blnPassesImageValidation = true; // Checks that file is an image, and also that it is < 1 MB

            if (img.HasFile && img.PostedFile != null)
            {
                string strFileExtension = Path.GetExtension(img.PostedFile.FileName.ToString());

                if (!chkValidExtension(strFileExtension))
                {
                    lblFileUploadError.Text += "'" + strFileExtension + "'" + " Format is not allowed. <br>";
                    blnPassesImageValidation = false;
                }

                // Create a FILE
                HttpPostedFile File = fileUploadLocationPic.PostedFile;
                // Create byte array
                imgByte = new Byte[File.ContentLength];
                // force control to load data
                File.InputStream.Read(imgByte, 0, File.ContentLength);

最后使用UPDATE语法将imgByte保存到数据库。

0 个答案:

没有答案