上传文件内容预览

时间:2013-03-21 04:40:33

标签: asp.net

在我的asp.net网络应用程序中,我想要第三方文件内容预览工具,通过使用该工具,我将显示上传的文件(如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)我想在预览中显示文件数据。

1 个答案:

答案 0 :(得分: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>

我们有OnUploadedComplete事件,这是javascript文件的函数名称。还有用于服务器端处理的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