zoukankan      html  css  js  c++  java
  • 利用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());
                    }

                }
            }

  • 相关阅读:
    js里的稀疏数组
    JS中二进制与十进制的相互转换
    【leetcode-03】给定一个字符串,请你找出其中不含有重复字符的最长子串的长度
    JavaScipt30(第二十二个案例)(主要知识点:getBoundingClientRect)
    JavaScipt30(第十八个案例)(主要知识点:Array.prototype.map)
    JavaScipt30(第十个案例)(主要知识点:选中一个数组中间相连部分进行操作的一种思路)
    JavaScipt30(第八个案例)(主要知识点:canvas)
    Lydsy2017省队十连测
    几个多项式的题
    poj3294Life Forms
  • 原文地址:https://www.cnblogs.com/hqbird/p/1287935.html
Copyright © 2011-2022 走看看