.net mvc 上传PPT文件转化为图片解决方案

来源:互联网 发布:2.5d弧面玻璃淘宝有吗 编辑:程序博客网 时间:2024/05/17 03:29

后台代码:

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using System.Threading;
using System.IO;


namespace ppt2imageDemo.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/


        public ActionResult Index()
        {
            return View();
        }


        public JsonResult PushFilesAjax() {
            var file = Request.Files[0];
            var path = AppDomain.CurrentDomain.BaseDirectory + "imagesfromppt/" + file.FileName;
            var savepath = AppDomain.CurrentDomain.BaseDirectory + "ppt/";


            if(!System.IO.Directory.Exists(path)){
                System.IO.Directory.CreateDirectory(path);
            }
            if (!System.IO.Directory.Exists(savepath))
            {
                System.IO.Directory.CreateDirectory(savepath);
            }
            var filepath = Path.Combine(savepath, file.FileName);
            file.SaveAs(filepath);
            Microsoft.Office.Interop.PowerPoint.Application application = null;
            Presentation persentation = null;
            try
            {
                application = new Microsoft.Office.Interop.PowerPoint.Application();
                persentation = application.Presentations.Open(filepath);
                //persentation.Slides[1].Export(path + "\\page" + 1 + ".jpg", "JPG", 800, 600);
                for (var k = 1; k <= persentation.Slides.Count;k++ )
                {
                    persentation.Slides[k].Export(path + "\\page" + k + ".jpg", "JPG", 800, 600);
                }


            }
            catch(Exception e) {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (persentation != null)
                {
                    persentation.Close();
                    persentation = null;
                }
                if (application != null)
                {
                    application.Quit();
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

            return Json(new { success=true});
        }
    }
}
原创粉丝点击