public ResultMsg_FileUPload doUploadFile(ResultMsg_FileUPload result, HttpPostedFile file) { //判断文件是否为空 if (file != null) { string fileName = file.FileName; string fileType = Path.GetExtension(fileName).ToLower(); //由于不同浏览器取出的FileName不同(有的是文件绝对路径,有的是只有文件名),故要进行处理 if (fileName.IndexOf(' ') > -1) { fileName = fileName.Substring(fileName.LastIndexOf(' ') + 1); } else if (fileName.IndexOf('/') > -1) { fileName = fileName.Substring(fileName.LastIndexOf('/') + 1); } if (!Directory.Exists(uploadFilePath)) { Directory.CreateDirectory(uploadFilePath); } result.sourceFileName = fileName; result.targetFileName = getTemporaryFileName(fileType); file.SaveAs(uploadFilePath + result.targetFileName); result.result = "success"; result.msg = "上传成功"; } else { result.result = "failed"; result.msg = "上传失败"; result.sourceFileName = ""; result.targetFileName = ""; } return(result); }
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Session["UserName"] != null) { UserName = context.Session["UserName"].ToString().ToUpper().Trim(); } else { UserName = ""; } Action = RequstString("Action"); if (Action.Length == 0) { Action = ""; } if (Action == "Equ_Detail") { EquInfo equinfo = new EquInfo(); equinfo.ID = RequstString("EquID"); EquInfo result = new EquInfo(); result = GetEquDetailObj(equinfo, result); context.Response.Write(jsc.Serialize(result)); } else if (Action == "PartsFileUploadify" || Action == "ManuUploadify") { HttpPostedFile file = System.Web.HttpContext.Current.Request.Files["Filedata"]; ResultMsg_FileUPload result = new ResultMsg_FileUPload(); result = doUploadFile(result, file); context.Response.Write(jsc.Serialize(result)); } else if (Action == "Equ_Add") { EquInfo dataEntity = new EquInfo(); //dataEntity.ID = RequstString("ProcId"); dataEntity.ProcessCode = RequstString("ProcessName"); dataEntity.DeviceCode = RequstString("DeviceCode"); dataEntity.DeviceName = RequstString("DeviceName"); dataEntity.DeviceVendor = RequstString("DeviceVendor"); dataEntity.DeviceUseDate = RequstString("PmStartDate"); dataEntity.DevicePartsFile = RequstString("DevicePartsFile"); dataEntity.DeviceManualFile = RequstString("DeviceManualFile"); dataEntity.DeviceComment = RequstString("Description"); dataEntity.UploadedFile = RequstString("UploadedFile"); dataEntity.UploadedManualFile = RequstString("UploadedManualFile"); ResultMsg_Equ result = new ResultMsg_Equ(); result = addEquDataInDB(dataEntity, result); context.Response.Write(jsc.Serialize(result)); } else if (Action == "Equ_Edit") { EquInfo dataEntity = new EquInfo(); dataEntity.ID = RequstString("EquID"); dataEntity.ProcessCode = RequstString("ProcessName"); dataEntity.DeviceCode = RequstString("DeviceCode"); dataEntity.DeviceName = RequstString("DeviceName"); dataEntity.DeviceVendor = RequstString("DeviceVendor"); dataEntity.DeviceUseDate = RequstString("PmStartDate"); dataEntity.DevicePartsFile = RequstString("DevicePartsFile"); dataEntity.DeviceManualFile = RequstString("DeviceManualFile"); dataEntity.DeviceComment = RequstString("Description"); dataEntity.UploadedFile = RequstString("UploadedFile"); dataEntity.UploadedManualFile = RequstString("UploadedManualFile"); ResultMsg_Equ result = new ResultMsg_Equ(); result = editEquDataInDB(dataEntity, result); context.Response.Write(jsc.Serialize(result)); } else if (Action == "PartsFileCHECK" || Action == "PartsFileDOWNLOAD") { string objID = RequstString("ObjID"); string fileName = GetEquPartsFileNameFromDB(objID); string fileType = Path.GetExtension(fileName).ToLower(); string fileWithoutType = Path.GetFileNameWithoutExtension(fileName); if (!Directory.Exists(browsePartsFilePath)) { Directory.CreateDirectory(browsePartsFilePath); } context.Response.ClearContent(); context.Response.ClearHeaders(); if (Action == "PartsFileCHECK") { //context.Response.AppendHeader("Content-Disposition", string.Format("inline;filename={0}", fileName)); if (fileType == ".xlsx" || fileType == ".xls") { string path = browsePartsFilePath + Common.StringFilter.FilterSpecial(fileName); try { Workbook wb = new Workbook(path); wb.Save(browsePartsFilePath + fileWithoutType + ".pdf", Aspose.Cells.SaveFormat.Pdf); context.Response.Write("./PartsFile/" + fileWithoutType + ".pdf"); } catch (Exception ex) { context.Response.Write("false"); } } else { context.Response.Write("./PartsFile/" + fileWithoutType + ".pdf"); } } else { context.Response.AppendHeader("Content-Disposition", string.Format("attached;filename={0}", HttpContext.Current.Server.UrlEncode(fileName.ToString()))); if (fileType == ".docx" || fileType == ".doc") { context.Response.AppendHeader("content-type", "application/msword"); } else if (fileType == ".xlsx" || fileType == ".xls") { context.Response.AppendHeader("content-type", "application/x-msexcel"); } else if (fileType == ".pdf") { context.Response.AppendHeader("content-type", "application/pdf"); } context.Response.ContentType = "application/octet-stream"; context.Response.ContentEncoding = System.Text.Encoding.Default; try { FileInfo fileInfo = new FileInfo(browsePartsFilePath + Common.StringFilter.FilterSpecial(fileName)); context.Response.AddHeader("content-length", fileInfo.Length.ToString());//文件大小 context.Response.WriteFile(browsePartsFilePath + Common.StringFilter.FilterSpecial(fileName)); } catch (Exception ex) { context.Response.Write(ex.Message); } context.Response.Flush(); context.Response.Close(); } } else if (Action == "ManuCHECK" || Action == "ManuDOWNLOAD") { string objID = RequstString("ObjID"); string fileName = GetEquManuFileNameFromDB(objID); string fileType = Path.GetExtension(fileName).ToLower(); FileInfo fileInfo = new FileInfo(browseManualFilePath + fileName); string fileWithoutType = Path.GetFileNameWithoutExtension(fileName); if (!Directory.Exists(browsePartsFilePath)) { Directory.CreateDirectory(browsePartsFilePath); } context.Response.ClearContent(); context.Response.ClearHeaders(); if (Action == "ManuCHECK") { if (fileType == ".docx" || fileType == ".doc") { string path = browseManualFilePath + Common.StringFilter.FilterSpecial(fileName); try { //读取doc文档 Document doc = new Document(path); ////保存为PDF文件,此处的SaveFormat支持很多种格式,如图片,epub,rtf 等等 doc.Save(browseManualFilePath + fileWithoutType + ".pdf", Aspose.Words.SaveFormat.Pdf); //Workbook wb = new Workbook(path); //wb.Save(browseManualFilePath + fileWithoutType + ".pdf", SaveFormat.Pdf); context.Response.Write("./ManualFile/" + fileWithoutType + ".pdf"); } catch (Exception ex) { context.Response.Write("false"); } } else { context.Response.Write("./ManualFile/" + fileWithoutType + ".pdf"); } } else { //context.Response.AppendHeader("Content-Disposition", string.Format("attached;filename={0}", fileName)); //context.Response.AddHeader("content-length", fileInfo.Length.ToString());//文件大小 context.Response.AppendHeader("Content-Disposition", string.Format("attached;filename={0}", HttpContext.Current.Server.UrlEncode(fileName.ToString()))); if (fileType == ".docx" || fileType == ".doc") { context.Response.AppendHeader("content-type", "application/msword"); } else if (fileType == ".xlsx" || fileType == ".xls") { context.Response.AppendHeader("content-type", "application/x-msexcel"); } else if (fileType == ".pdf") { context.Response.AppendHeader("content-type", "application/pdf"); } try { if (!Directory.Exists(browseManualFilePath)) { Directory.CreateDirectory(browseManualFilePath); } FileInfo fileInfo1 = new FileInfo(browseManualFilePath + fileName); context.Response.AddHeader("content-length", fileInfo1.Length.ToString());//文件大小 context.Response.WriteFile(browseManualFilePath + fileName); } catch (Exception ex) { context.Response.Write(ex.Message); } context.Response.Flush(); context.Response.Close(); } } }