使用WebService实现文件上传
发布时间:2020-12-16 23:55:41 所属栏目:安全 来源:网络整理
导读:使用WebService实现文件上传 ? ? ? ?使用WebService实现文件上传,具体方法如下: ? ? ?? ? ? ? ?//文件引用 ? ? ? using System.IO; ? ? ? using System.Drawing; ? ? ? using System.Configuration; ? ? ? ?//日志引用 ? ? ? ?using Fx678Member.Framework.
使用WebService实现文件上传
? ? ? ?使用WebService实现文件上传,具体方法如下:
? ? ??
? ? ? ?//文件引用
? ? ? using System.IO; ? ? ? using System.Drawing; ? ? ? using System.Configuration; ? ? ? ?//日志引用 ? ? ? ?using Fx678Member.Framework.Log; ? ??
? ? ? ??#region 全局变量
? ? ? ? /// <summary> ? ? ? ? /// ImageType 文件类型 ? ? ? ? /// </summary> ? ? ? ? List<String> ImageType = new List<string>() { }; ? ? ? ? /// <summary> ? ? ? ? /// FileType 文件类型 ? ? ? ? /// </summary> ? ? ? ? List<String> FileType = new List<string>() { }; ? ? ? ? /// <summary> ? ? ? ? /// 网站域名配置 ? ? ? ? /// </summary> ? ? ? ? String WebsiteName = String.Empty; ? ? ? ? #endregion ? ? ? ? /// <summary> ? ? ? ? /// 文件上传的方法 ? ? ? ? /// </summary> ? ? ? ? /// <param name="fileType">文件后缀名</param> ? ? ? ? /// <param name="GetBinaryFile">文件数据组</param> ? ? ? ? /// <param name="WebSiteName">站点名</param> ? ? ? ? /// <returns></returns> ? ? ? ? [WebMethod(Description = "WebService 文件上传")] ? ? ? ? public string Upload(String fileType,byte[] GetBinaryFile,String WebSiteName) ? ? ? ? { ? ? ? ? ? ? String result = String.Empty; ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? //获取配置的图片、文件类型 ? ? ? ? ? ? ? ? ImageType = ConfigurationManager.AppSettings["Image"].Split('|').ToList(); ? ? ? ? ? ? ? ? FileType = ConfigurationManager.AppSettings["File"].Split('|').ToList(); ? ? ? ? ? ? ? ? WebsiteName = ConfigurationManager.AppSettings["WebSiteName"].ToString(); ? ? ? ? ? ? ? ? //判断文件类型是否符合要求 ? ? ? ? ? ? ? ? if (ImageType.Contains(fileType)) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? //上传的为图片 ? ? ? ? ? ? ? ? ? ? result = UploadImage(fileType,GetBinaryFile,WebSiteName); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else if (FileType.Contains(fileType)) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? //上传的为文件 ? ? ? ? ? ? ? ? ? ? result = UploadFile(fileType,WebSiteName); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? result = "Error:很遗憾文件格式有误! n" + ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"支持的图片格式为:" + String.Join("|",ImageType) + "n" + ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"支持的文件格式为:" + String.Join("|",FileType); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception ex) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Logger.Error("Upload 文件上传的方法异常",ex); ? ? ? ? ? ? ? ? result = "Error:Upload 文件上传的方法异常"; ? ? ? ? ? ? } ? ? ? ? ? ? //返回结果 ? ? ? ? ? ? return result; ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 图片上传 ? ? ? ? /// </summary> ? ? ? ? /// <param name="fileType">文件后缀名</param> ? ? ? ? /// <param name="GetBinaryFile">文件字节数组</param> ? ? ? ? /// <param name="Website">站点名</param> ? ? ? ? /// <returns></returns> ? ? ? ? private String UploadImage(String fileType,String Website) ? ? ? ? { ? ? ? ? ? ? String result = String.Empty; ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? byte[] image = GetBinaryFile; ? ? ? ? ? ? ? ? if (image != null && image.Length > 0) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? //判断图片大小 ? ? ? ? ? ? ? ? ? ? int size = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ImageSize"].ToString()); ? ? ? ? ? ? ? ? ? ? if (image.Length <= size) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? //定义Bitmap对象 ? ? ? ? ? ? ? ? ? ? ? ? Bitmap bmp = new Bitmap(new System.IO.MemoryStream(image)); ? ? ? ? ? ? ? ? ? ? ? ? //保存 ? ? ? ? ? ? ? ? ? ? ? ? String file = GetFileSrc("Image",Website) + "/" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileType; ? ? ? ? ? ? ? ? ? ? ? ? bmp.Save(Server.MapPath(file)); ? ? ? ? ? ? ? ? ? ? ? ? //判断是否成功 ? ? ? ? ? ? ? ? ? ? ? ? if (File.Exists(Server.MapPath(file))) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? //保存成功,返回绝对地址 ? ? ? ? ? ? ? ? ? ? ? ? ? ? result = "Success:" + WebsiteName + "/" + file; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? //保存失败 ? ? ? ? ? ? ? ? ? ? ? ? ? ? result = "Error:图片文件保存失败!"; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? result = "Error:图片大小不超过" + size + "字节(Byte)"; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? result = "Error:文件路径有误!"; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception ex) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Logger.Error("UploadImage 图片上传异常",ex); ? ? ? ? ? ? ? ? result = "Error:UploadImage 图片上传异常!n详细请参考日志信息"; ? ? ? ? ? ? } ? ? ? ? ? ? return result; ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 文件上传 ? ? ? ? /// </summary> ? ? ? ? /// <param name="fileType">后缀名</param> ? ? ? ? /// <param name="src">地址</param> ? ? ? ? /// <param name="Website">站点域名</param> ? ? ? ? /// <returns></returns> ? ? ? ? private String UploadFile(String fileType,String Website) ? ? ? ? { ? ? ? ? ? ? String result = String.Empty; ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? byte[] file = GetBinaryFile; ? ? ? ? ? ? ? ? if (file != null && file.Length > 0) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? //判断文件大小 ? ? ? ? ? ? ? ? ? ? int size = int.Parse(System.Configuration.ConfigurationManager.AppSettings["FileSize"].ToString()); ? ? ? ? ? ? ? ? ? ? if (file.Length <= size) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? //保存 ? ? ? ? ? ? ? ? ? ? ? ? String filep = GetFileSrc("File",Website) + "/" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileType; ? ? ? ? ? ? ? ? ? ? ? ? String filename = Server.MapPath(filep); ? ? ? ? ? ? ? ? ? ? ? ? using (FileStream filestream = new FileStream(filename,FileMode.Create,FileAccess.Write)) ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? //写入文件 ? ? ? ? ? ? ? ? ? ? ? ? ? ? filestream.Write(file,file.Length); ? ? ? ? ? ? ? ? ? ? ? ? ? ? filestream.Flush(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? //关闭 ? ? ? ? ? ? ? ? ? ? ? ? ? ? filestream.Close(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? //判断是否成功 ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (File.Exists(filename)) ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? result = "Success:" + WebsiteName + "/" + filep; ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? result = "Error:文件上传失败!请稍后再试"; ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? result = "UploadFile 上传的文件大小不能超过" + size + "字节(Byte)"; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? result = "Error:文件路径有误!"; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception ex) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Logger.Error("UploadFile 文件上传异常!",ex); ? ? ? ? ? ? ? ? result = "UploadFile 文件上传异常!"; ? ? ? ? ? ? } ? ? ? ? ? ? return result; ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 获取保存路径 ? ? ? ? /// </summary> ? ? ? ? /// <param name="type">文件类型:Image File</param> ? ? ? ? /// <param name="Website">域名</param> ? ? ? ? /// <returns></returns> ? ? ? ? private String GetFileSrc(String type,String Website) ? ? ? ? { ? ? ? ? ? ? //判断该域名下,是否有对应文件夹 ? ? ? ? ? ? String dt = DateTime.Now.ToString("yyyyMMdd"); ? ? ? ? ? ? String path = Website + "_" + type + "/" + dt; ? ? ? ? ? ? if (!Directory.Exists(Server.MapPath(path))) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Directory.CreateDirectory(Server.MapPath(path)); ? ? ? ? ? ? } ? ? ? ? ? ? return path; ? ? ? ? }
? ? ? 功能完成后部署到IIS上,调用即可。方法如下:
? ? ???//文件流
? ? ? ?byte[] myFile = ConvertFileToByteBuffer(this.fupImg1.PostedFile.InputStream);
? ? ? ?
? ? ? ?//文件类型(文件后缀名)
? ? ? ?String filetype = System.IO.Path.GetExtension(this.fupImg1.FileName);
? ? ??//调用服务
? ? ? String result = service.Upload(filetype,myFile,Websitename); ? ? ? ?
? ??
? ??#region 返回指定文件流数组 ? ? /// <summary> ? ? /// 返回指定文件流数组 ? ? /// </summary> ? ? /// <param name="stream"></param> ? ? /// <returns></returns> ? ? private byte[] ConvertFileToByteBuffer(Stream stream) ? ? { ? ? ? ? int b = new int(); ? ? ? ? MemoryStream memostream = new MemoryStream(); ? ? ? ? while ((b = stream.ReadByte()) != -1) ? ? ? ? { ? ? ? ? ? ? memostream.WriteByte((byte)b); ? ? ? ? } ? ? ? ? return memostream.ToArray(); ? ? } ? ? #endregion
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |