加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webService上传图片

发布时间:2020-12-17 00:56:33 所属栏目:安全 来源:网络整理
导读:webService? ? /// summary ? ? /// ?上传图片webServer 的摘要说明 ? ? /// /summary ? ? [WebService(Namespace = "http://tempuri.org/")] ? ? [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] ? ? [ToolboxItem(false)] ? ? public clas

webService?

?/// <summary>
? ? /// ?上传图片webServer 的摘要说明
? ? /// </summary>
? ? [WebService(Namespace = "http://tempuri.org/")]
? ? [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
? ? [ToolboxItem(false)]
? ? public class WebService1 : System.Web.Services.WebService
? ? {
? ? ? ? [WebMethod]
? ? ? ? public bool UpdateFile(byte[] content,string pathand,string filename)
? ? ? ? {
? ? ? ? ? ? string pathandname = pathand + filename;
? ? ? ? ? ? int index = pathandname.LastIndexOf(".");
? ? ? ? ? ? if (index == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? string extended = string.Empty;
? ? ? ? ? ? ? ? if (index + 1 == pathandname.Length)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? extended = pathandname.Substring(index + 1);
? ? ? ? ? ? ? ? ? ? if (extended == "jpeg" || extended == "gif" || extended == "jpg" || extended == "bmp" || extended == "png")
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (!Directory.Exists(@pathand))//若文件夹不存在则新建文件夹 ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Directory.CreateDirectory(@pathand); //新建文件夹 ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? }?


? ? ? ? ? ? ? ? ? ? ? ? ? ? //File.WriteAllBytes(Server.MapPath(pathandname),content);
? ? ? ? ? ? ? ? ? ? ? ? ? ? File.WriteAllBytes(pathandname,content);
? ? ? ? ? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }

//测试

? private void btnSaveServer_Click(object sender,EventArgs e)
? ? ? ? {
? ? ? ? ? ? OpenFileDialog fileDialog = new OpenFileDialog();
? ? ? ? ? ? if (fileDialog.ShowDialog() == DialogResult.OK)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? string pathand = CommonClass.Config.GetAppSettings<string>("ProductImageUrl",@"D:FSTERPProductImage");
? ? ? ? ? ? ? ? string imagename = "mylove";
? ? ? ? ? ? ? ? bool uploadResult = UploadImageWebService(fileDialog.FileName,pathand,imagename);
? ? ? ? ? ? ? ? if (uploadResult)
? ? ? ? ? ? ? ? ? ? MessageBox.Show("上传成功!");
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? MessageBox.Show("上传失败!");
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 上传图片[通过webServer]
? ? ? ? /// </summary>
? ? ? ? /// <param name="filename">选择图片路径[默认选择文件包括后缀名]</param>
? ? ? ? /// <param name="pathand">上传服务器文件夹[文件夹不存在则新建]</param>
? ? ? ? /// <param name="imagename">上传后图片文件名[不包括后缀名]</param>
? ? ? ? /// <returns>上传结果</returns>
? ? ? ? public bool UploadImageWebService(string filename,string imgname)
? ? ? ? {
? ? ? ? ? ??
? ? ? ? ? ? string extension = Path.GetExtension(filename).ToLower().Replace(".","");
? ? ? ? ? ? string paramSuffix = "|" + CommonClass.Config.GetAppSettings<string>("ImageFormat","jpg|jpge|gif|bmp|png") + "|";
? ? ? ? ? ? int pi = paramSuffix.IndexOf("|" + extension + "|");
? ? ? ? ? ? if (pi < 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("仅能上传jpg|jpge|gif|bmp|png格式的图片!");
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? FileInfo fileInfo = new FileInfo(filename);
? ? ? ? ? ? ? ? if (fileInfo.Length > 20480)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? MessageBox.Show("上传的图片不能大于20K");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? //Stream file = fileDialog.OpenFile();
? ? ? ? ? ? ? ? ? ? FileStream file = new FileStream(filename,FileMode.Open,FileAccess.Read);
? ? ? ? ? ? ? ? ? ? byte[] bytes = new byte[file.Length];
? ? ? ? ? ? ? ? ? ? file.Read(bytes,bytes.Length);
? ? ? ? ? ? ? ? ? ? //实例化WebService服务。ServiceReference1是我们在添加引用时设置的命名空间
? ? ? ? ? ? ? ? ? ? WebService.WebService1 webservice = new FSTERP.WebService.WebService1();
? ? ? ? ? ? ? ? ? ? DateTime time = DateTime.Now;
? ? ? ? ? ? ? ? ? ? //重命名图片的名称与路径
? ? ? ? ? ? ? ? ? ? //string pathand = CommonClass.Config.GetAppSettings<string>("ProductImageUrl",@"D:FSTERPProductImage");
? ? ? ? ? ? ? ? ? ? string imagename = imgname + "." + extension;
? ? ? ? ? ? ? ? ? ? //string pathandname = pathand + imagename;
? ? ? ? ? ? ? ? ? ? if (webservice.UpdateFile(bytes,imagename))
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? return false;
? ? ? ? }


测试图片

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读