利用webservices实现图片远程上传

首先建立webservice环境,创建实现函数

 

 [WebService(Namespace = "http://jason.han%22,description/="在WebSerive 上传图片")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class WebServiceUpload : System.Web.Services.WebService
    {

        [WebMethod(Description = "上传服务器图片信息,返回是否成功")]
        public string UploadFile(byte[] fs,string FileName)
        {
            try
            {
                MemoryStream m = new MemoryStream(fs);//定义并实例化一个内存流,来存放上传的图片二进制流
                FileStream f = new FileStream(Server.MapPath(".") + \\images" + FileName, FileMode.Create);//把内存里的文件写入文件流
                m.WriteTo(f);
                m.Close();
                f = null;
                m = null;
                return "文件上传成功";
            }
            catch(Exception ex)
            {
                return ex.Message.ToString();
            }
        }
    }

前端文件上传方法实现

 

protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.PostedFile != null)
            {
                System.Web.HttpFileCollection oFiles;
                oFiles = System.Web.HttpContext.Current.Request.Files;
                if (oFiles.Count < 1)
                {
                    Response.Write("请选择文件");
                    Response.End();
                }
                string FilePath = oFiles[0].FileName;
                if (FilePath == null || FilePath == "")
                {
                    Response.Write("请重新选择图片");
                    Response.End();
                }
                string fileName = FilePath.Substring(FilePath.LastIndexOf("\\") + 1);
                try
                {
                    byte[] b = new byte[oFiles[0].ContentLength];
                    System.IO.Stream fs;
                    PicUploadDemo.WebServiceUpload oWSUpload;//调用webservices
                    oWSUpload = new WebServiceUpload();
                    fs = (System.IO.Stream)oFiles[0].InputStream;
                    fs.Read(b, 0, oFiles[0].ContentLength);//将输入流读入二进制数组中
                    Response.Write(oWSUpload.UploadFile(b,fileName));
                    fs.Close();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message.ToString());
                }

            }
        }

转载于:https://www.cnblogs.com/hqbird/archive/2008/09/09/1287935.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值