0

我正在使用以下教程链接在我的 Web 项目中使用它,以便我可以在.swf扩展名中验证 Flash 文件的高度和宽度。

我上传闪存文件并检查所需的高度和高度,如果它不在所需的尺寸内,那么我必须删除文件并显示用户验证错误消息。

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

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

我使用以下 Close & 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();
    }

}
4

0 回答 0