后台代码:
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});
}
}
}