using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using Spire.Doc; using Spire.Doc.Documents; using System.Reflection; namespace SuperMap.RealEstate.HighWay.Modules.ContractTemplated { public partial class Upload : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Attributes["onclick"] = "return checkform();"; } protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { string upPath = "/UploadImageDir/"; //上传文件路径 int upLength = 5; //上传文件大小 //string upFileType = "|image/bmp|image/x-png|image/pjpeg|image/gif|image/png|image/jpeg|"; string fileContentType = FileUpload1.PostedFile.ContentType; //文件类型 string name = FileUpload1.PostedFile.FileName; // 客户端文件路径 FileInfo file = new FileInfo(name); string fileName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + file.Extension; // 文件名称,当前时间(yyyyMMddhhmmssfff) string webFilePath = Server.MapPath(upPath) + fileName; // 服务器端文件路径 string FilePath = upPath + fileName; //页面中使用的路径 if (!File.Exists(webFilePath)) { if ((FileUpload1.FileBytes.Length / (1024 * 1024)) > upLength) { ClientScript.RegisterStartupScript(this.GetType(), "upfileOK", "alert('大小超出 " + upLength + " M的限制,请处理后再上传!');", true); return; } try { FileUpload1.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件 Spire.Doc.Document doc = new Spire.Doc.Document(); doc.LoadFromFile(webFilePath); //PictureWatermark picture = new PictureWatermark(); //picture.Picture = System.Drawing.Image.FromFile(Server.MapPath("~/CreateWord/WaterMarkImage/watermark.png")); //picture.Scaling = 80; //doc.Watermark = picture; doc.SaveToFile(webFilePath); ClientScript.RegisterStartupScript(this.GetType(), "upfileOK", "alert('提示:文件上传成功');", true); } catch (Exception ex) { ClientScript.RegisterStartupScript(this.GetType(), "upfileOK", "alert('提示:文件上传失败" + ex.Message + "');", true); } } else { ClientScript.RegisterStartupScript(this.GetType(), "upfileOK", "alert('提示:文件已经存在,请重命名后上传');", true); } } } } }