0

次のチュートリアルリンクを使用してWebプロジェクトで使用しているので、拡張子のフラッシュファイルの高さと幅を検証できます.swf

フラッシュファイルをアップロードし、必要な高さを確認します。必要な寸法内にない場合は、ファイルを削除してユーザー検証エラーメッセージを表示する必要があります。

ファイルを削除している間、次のエラーメッセージが生成されます

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();

       }

画像とフラッシュファイルをアップロードするための完全なコード

   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