这些天一直在捣鼓一个东西,就是上传word文件,然后将它预览出来,搞得我有点心烦。
首先前台页面要获取得到的文件名,然后触发上传事件
@*//用于接收path路径 *@
<input type="hidden" id="path" value="@ViewData["path"]" />
<div id="upload" style="width: 300px; display: block; padding-bottom: 15px; padding-top: 15px;">
<input type="file" class="easyui-linkbutton" name="uploadfile" id="uploadfile"
multiple="multiple" onchange="fileSelected();" />
</div>
@*添加按钮弹出框*@
<div style="margin-bottom: 5px; margin-top: 20px;">
<a id="" class="easyui-linkbutton" href="#" data-options="plain:true,iconCls:'icon-undo'" style="margin-left: 5px;" onclick="uploadFile()">
<span>上传</span>
</a>
</div>
js中的uploadFile
function uploadFile() {
var fd = new FormData();
fd.append("uploadfile", document.getElementById('uploadfile').files[0]);
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false); //上传进度
xhr.addEventListener("load", uploadComplete, false); //上传完成
xhr.addEventListener("error", uploadFailed, false); //上传出错
xhr.addEventListener("abort", uploadCanceled, false); //取消上传
xhr.addEventListener("success", uploadSuccess, false);//上传成功
xhr.open("POST", "/TeaQueryHomework/UploadFile"); //Controller里边UploadFile方法
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200)
{
var data = xhr.responseText;
var content = JSON.parse(data);
alert(content);
}
};
xhr.send(fd);
}
调用controller中的方法,因为要预览,所有只上传文件还是不行的,这里用到了一个flexpaper插件,将office文件转换为pdf文件,再将pdf文件转为swf文件,才可以被flexpaper预览。
public ActionResult UploadFile(HttpPostedFileBase[] fileToUpload)
{
//变量定义pdf转为swf的工具路径
string pdf2swfToolPath = System.Web.HttpContext.Current.Server.MapPath("~/FlexPaper/pdf2swf.exe");
//定义保存路径的变量
string OfficeFilePath = Server.MapPath("~/Content/TeaFile/OFFICE/");
string PdfFilePath = Server.MapPath("~/Content/TeaFile/PDF/");
string SWFFilePath = Server.MapPath("~/Content/TeaFile/SWF/");
string SwfFileName = String.Empty;
//上传word文件
HttpPostedFileBase uploadfile = Request.Files["uploadfile"];
if (uploadfile == null || string.IsNullOrEmpty(uploadfile.FileName))
{
return Json("请选择上传文件!");
}
//取得上传文件之扩展文件名,并转换成小写字母
string fileExtension = System.IO.Path.GetExtension(uploadfile.FileName).ToLower();
if (fileExtension != ".docx")
{
return Json("只能上传word文件!");
}
////存储文件到文件夹
uploadfile.SaveAs(Server.MapPath("../Content/TeaFile/OFFICE/" + uploadfile.FileName));
string PdfFileName = OfficeToPdf(OfficeFilePath, uploadfile.FileName, PdfFilePath);
SwfFileName = PdfToSwf(pdf2swfToolPath, PdfFilePath, PdfFileName, SWFFilePath);
ViewData["path"] = SWFFilePath;
HttpCookie cookie = new HttpCookie("path_Id");
//向cookie中传时进行编码(防止中文乱码)
string path2 = System.Web.HttpUtility.UrlEncode(SwfFileName, Encoding.UTF8);
cookie.Value = path2;
//添加cookie
Response.Cookies.Add(cookie);
return Json("上传成功!");
}
OfficeToPdf-将office文件转化为pdf文件
private string OfficeToPdf(string OfficePath, string OfficeName, string destPath)
{
string fullPathName = OfficePath + OfficeName;//包含 路径 的全称
string fileNameWithoutEx = System.IO.Path.GetFileNameWithoutExtension(OfficeName);//不包含路径,不包含扩展名
string extendName = System.IO.Path.GetExtension(OfficeName).ToLower();//文件扩展名
string saveName = destPath + fileNameWithoutEx + ".pdf";
string returnValue = fileNameWithoutEx + ".pdf";
switch (extendName)
{
case ".doc":
PreviewConvert.WordToPDF(fullPathName, saveName);
break;
case ".docx":
PreviewConvert.WordToPDF(fullPathName, saveName);
break;
case ".xls":
PreviewConvert.ExcelToPDF(fullPathName, saveName);
break;
case ".xlsx":
PreviewConvert.ExcelToPDF(fullPathName, saveName);
break;
default:
returnValue = "";
break;
}
return returnValue;
}
PdfToSwf-将pdf文件转化为swf文件private string PdfToSwf(string pdf2swfPath, string PdfPath, string PdfName, string destPath)
{
string fullPathName = PdfPath + PdfName;//包含 路径 的全称
string fileNameWithoutEx = System.IO.Path.GetFileNameWithoutExtension(PdfName);//不包含路径,不包含扩展名
string extendName = System.IO.Path.GetExtension(PdfName).ToLower();//文件扩展名
string saveName = destPath + fileNameWithoutEx + ".swf";
string returnValue = fileNameWithoutEx + ".swf";
if (extendName != ".pdf")
{
returnValue = "";
}
else
{
PreviewConvert.PDFToSWF(pdf2swfPath, fullPathName, saveName);
}
return returnValue;
}
前期工作做得差不错了,剩下的就是从cookie中获取数据,打开flexpaper还有设置一些它的属性
function Preview() {
//用cookie获取存储的文件名
var path = getCookie("path_Id");
var test = $("#uploadfile").val();
$("#dlgPreview").dialog('open');
var target = "../../Content/TeaFile/SWF/" + path;
var fp = new FlexPaperViewer(
'../../FlexPaper/FlexPaperViewer', /* 对应FlexPaperViewer.swf文件*/
'viewerPlaceHolder', {config : {
SwfFile: escape(target),
Scale : 0.6,
ZoomTransition : 'easeOut',
ZoomTime : 0.5,
ZoomInterval : 0.2,
FitPageOnLoad : true,
FitWidthOnLoad : true,
FullScreenAsMaxWindow : false,
ProgressiveLoading : false,
MinZoomSize : 0.2,
MaxZoomSize : 5,
SearchMatchAll : false,
InitViewMode : 'Portrait',
ViewModeToolsVisible : true,
ZoomToolsVisible : true,
NavToolsVisible : true,
CursorToolsVisible : true,
SearchToolsVisible : true,
localeChain: 'zh_CN'
}
});
$("#dlgPreview").dialog('open').dialog('setTitle', '预览');
}
function getCookie(Name) //cookies读取
{
var search = Name + "="
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf(";", offset)
if (end == -1) end = document.cookie.length
return decodeURIComponent(document.cookie.substring(offset, end))
}
else return ""
}
return ""
}