1

私のasp.netWebアプリケーションでは、サードパーティのファイルコンテンツプレビューツールが必要です。そのツールを使用して、アップロードされたファイル(jpg、tiff、xls、pdf、txt、docなど)を表示します。

protected void btn_upload_Click(object sender, EventArgs e)
{
    try
    {
        if (flup_upload.HasFile)
        {
            _fname = flup_upload.FileName.Replace(" ", "");
            string ext = Path.GetExtension(_fname).ToLower();

            if (ext == ".jpg" || ext == ".jpeg" || ext == ".bmp")
            {
               ------
            }
            else if (ext == ".xls" || ext == ".xlsx")
            {
               -----
            }
            else if (ext == ".pdf")
            {
               -----
            }
            else if (ext == ".tif" || ext == ".tiff")
            {
                ---
            }
            else if (ext == ".txt")
            {
               ----
            }
            else if (ext == ".docx" || ext == ".doc")
            {
               -----
            }
        }
    }
    catch (Exception ex)
    {
    }
}

つまり、ファイル(jpg / tiff / xls / pdf / txt / docなど)をアップロードした後、ファイルデータをプレビューで表示したいと思います。

4

1 に答える 1

0

ajaxcontroltoolkitファイルアップローダーを使用してそれを行うこともできます。

<td>
     <asp:Label runat="server" ID="myThrobber" Style="display: none; color: black;">
       UpLoading....
    </asp:Label>
    <br />
    <Ajax:AsyncFileUpload ID="FileUploadGaurdianPic" runat="server" ThrobberID="myThrobber"
      MaximumNumberOfFiles="1"
        OnClientUploadComplete="uploadComplete" OnUploadedComplete="FileUploadGaurdianPic_UploadedComplete" Style="margin-left: 0px" />
     <asp:Image runat="server" ID="imageGaurdainTemp" /> // temp image to show
                </td>

OnUploadedCompletejavascriptファイルの関数名であるイベントがあります。また、サーバー側の処理のためのUploaded_Completeイベント。

protected void FileUploadGaurdianPic_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
    // storing the file path in session, in-order to get this in futrue and isnert it into the database. note that i am only getting the path here;
    Session["GuardianPic"] = "/Resources/Images/guardian/" + e.FileName;
    SaveImage(e.FileName, FileUploadGaurdianPic, 2);
}
// here i am generating the thumbnail,and i am uploading the orignal image to the temp path, if you don't need this, remove it. I am only saving the thumbnail to the orignal path;
private void SaveImage(string FileName, AjaxControlToolkit.AsyncFileUpload uploader, int status)
{
    string path = String.Empty;
    switch (status)
    {
        case 1:
            path = Server.MapPath("~/Resources/Images/Student/");
            break;
        case 2:
            path = Server.MapPath("~/Resources/Images/guardian/");
            break;
    }

    string name = String.Empty;
    string storedPath = String.Empty;

    int intThumbWidth = 100;
    int intThumbHeight = 100;
    // Check whether the file is really a JPEG by opening it
    System.Drawing.Image.GetThumbnailImageAbort myCallBack =
                   new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);

    //Create Thumbnail
    Bitmap myBitmap = myBitmap = new Bitmap(uploader.PostedFile.InputStream); ;

    Bitmap map = new Bitmap(uploader.FileContent);
    // Save Thumbnail and Assign it to ThumbUrl out parameter
    System.Drawing.Image myThumbnail = myBitmap.GetThumbnailImage(intThumbWidth,
                                         intThumbHeight, myCallBack, IntPtr.Zero);
    try
    {
        string tempPath = Server.MapPath("/Resources/Images/temp/");
        if (!Directory.Exists(tempPath))
        {
            Directory.CreateDirectory(tempPath);
        }
        if (Directory.Exists(path))
        {
            name = Path.GetFileName(FileName);
            storedPath = path + name;
            myThumbnail.Save(storedPath);
            uploader.SaveAs(tempPath + name);
        }
        else
        {
            Directory.CreateDirectory(path);
            name = Path.GetFileName(FileName);
            storedPath = path + name;
            myThumbnail.Save(storedPath);
            uploader.SaveAs(tempPath + name);
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

最後に、画像を表示するjavascript関数。

function uploadComplete(sender, args) {
    var imgDisplay = $get("imageGaurdainTemp");
    var img = new Image();
    img.onload = function () {
        imgDisplay.style.cssText = "height:100px;width:100px";
        imgDisplay.src = img.src;
    };
    img.src = "/Resources/Images/guardian/" + args.get_fileName();

}

これがお役に立てば幸いです:D

于 2013-03-21T11:45:28.910 に答える