无法在验证失败后删除文件错误:'进程无法访问文件...'(SwfReader库)

时间:2012-12-09 08:21:14

标签: c# asp.net webforms

我正在使用以下Tutorial Link在我的网络项目中使用它,以便我可以验证身高和.swf扩展名中的Flash文件的宽度。

我上传了flash文件&检查所需的&高度,如果它不在所需的维度内,那么我必须删除文件&显示用户验证错误消息。

删除文件时会生成以下错误消息

The process cannot access the file 'C:\PROJECTS\xyz\images\banners\file.swf' because it is being used by another process.

我使用以下关闭& dispose方法释放对象但它仍然生成错误。我注意到确定使用哪种方法来释放对象,以便我可以删除文件,如果它不在必需的分区中

示例代码

    //Upload files
  string path = Server.MapPath("../images/banners/") + fileName;
  FileUploadBanner.PostedFile.SaveAs(path);
  Session["bannerImage"] = fileName.ToString();
    try
    {
            SwfReader swfReader = new SwfReader(path);
            Swf swf = swfReader.ReadSwf();
            int swfHeight =    swf.Header.Height;
            int swfWidth = swf.Header.Width;
            swfReader.Close();
            ((IDisposable)swfReader).Dispose();

            //   ((IDisposable)swfReader).Dispose();
               if ((swfHeight > int.Parse(hdnBannerHeight.Value)) || (swfWidth > int.Parse(hdnBannerWidth.Value)))
              {
                    lblImageUploadMessage1.Text = "File cant be upload as it has invalid dimensions";
                    lblImageUploadMessage1.CssClass = "lblErrorMSG";
                    Session["bannerImage"] = null;
                    if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); }
                 }
              }
        catch (Exception ex)
          { }
        finally
       {
         //((IDisposable)swfReader).Dispose();

       }

上传图片的完整代码& flash文件

   try
    {
        lblImageUploadMessage1.Text = "";
        String filePath = string.Empty;
        String CurrentGUID = Guid.NewGuid().ToString();
        string errMsg = string.Empty;

        if (FileUploadBanner.HasFile)
        {
            string fileName = System.IO.Path.GetFileName(FileUploadBanner.FileName);
            System.IO.FileInfo f = new System.IO.FileInfo(FileUploadBanner.PostedFile.FileName);
            double fileSize = (double)FileUploadBanner.FileBytes.Length;
            if (fileSize < 1024000) // 1 MB current size size in bytes 102400=100kb  512000 = 500kb
            {
                if ((f.Extension.ToLower() == ".jpg") || (f.Extension.ToLower() == ".png") || (f.Extension.ToLower() == ".gif") || (f.Extension.ToLower() == ".jpeg") || (f.Extension.ToLower() == ".swf"))
                {
                    fileName = CurrentGUID + f.Extension;
                    string fExtension = f.Extension.ToString();
                    filePath = Server.MapPath("../images/banners/") + fileName;

                    if (System.IO.File.Exists(filePath))
                    {
                        return;
                    }
                    else
                    {
                        //Upload files
                        string path = Server.MapPath("../images/banners/") + fileName;
                        FileUploadBanner.PostedFile.SaveAs(path);
                        Session["bannerImage"] = fileName.ToString();
                        errMsg = "Banner Uploaded Successfully";
                        lblImageUploadMessage1.Text = errMsg;
                        lblImageUploadMessage1.CssClass = "lblMSG";
                        if (f.Extension.ToLower() != ".swf")
                        {
                            System.Drawing.Image imgInfo = System.Drawing.Image.FromFile(filePath);
                            int imgH = imgInfo.Height;
                            int imgW = imgInfo.Width;
                            imgInfo.Dispose();
                            //Check Dimension of image & delete image if height,width is greater than specified  dimension
                            if ((imgH > int.Parse(hdnBannerHeight.Value)) || (imgW > int.Parse(hdnBannerWidth.Value)))
                            {
                                lblImageUploadMessage1.Text = "File cant be upload as it has invalid dimensions";
                                lblImageUploadMessage1.CssClass = "lblErrorMSG";
                                Session["bannerImage"] = null;
                                if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); }
                            }
                        }
                        //If Flash object
                        else
                        {
                           try{

                               lblImageUploadMessage1.Text = "You cant upload flash banner";
                               lblImageUploadMessage1.CssClass = "lblErrorMSG";

                               /* Not using below case as i not able to dispose the swfReader object which can result in unnecessary memory*/

                               ////// Create a swf stream reader
                               ////SwfReader swfReader = new SwfReader(path);
                               ////// Read the completed swf file
                               ////Swf swf = swfReader.ReadSwf();
                               ////// Read only headers of file to optimize read speed
                               //////Swf swf = swfReader.ReadSwfHeader();
                               ////int swfHeight = swf.Header.Height;
                               ////int swfWidth = swf.Header.Width;
                               ////swfReader.Close();
                               ////if ((swfHeight > int.Parse(hdnBannerHeight.Value)) || (swfWidth > int.Parse(hdnBannerWidth.Value)))
                               ////{
                               ////    lblImageUploadMessage1.Text = "File cant be upload as it has invalid dimensions";
                               ////    lblImageUploadMessage1.CssClass = "lblErrorMSG";
                               ////    Session["bannerImage"] = null;
                               ////    if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); }
                               ////}


                           }
                           catch (Exception ex)
                           {
                               lblErrorMessages.Text = ex.Message.ToString();
                           }
                           finally
                           {
                               //((IDisposable)swfReader).Dispose();
                           }


                        }


                    }
                    return;
                }
                //client-side error
                else
                {
                    errMsg = "File must be an Image type of .jpg, .png, .gif, .jpeg, .swf";
                    lblImageUploadMessage1.Text = errMsg;
                    return;
                }
            }
            else
            {
                errMsg = "File size is greater the 1MB";
                //client-side error
                lblImageUploadMessage1.Text = errMsg;
                return;
            }
        }
        else
        {
            errMsg = "Cant Upload File due to some error";
            //client-side error
            lblImageUploadMessage1.Text = errMsg;
            return;
        }
    }
    catch (Exception ex)
    {
        //Response.Write("ERROR MESSAGE : " + ex.Message.ToString());
        lblErrorMessages.Text = "ERROR MESSAGE : " +  ex.Message.ToString();
    }

}

0 个答案:

没有答案