通过WebService上传文件
昨天弄了一天的文件流,哎,现在还有点迷糊 实现WinForm客户端通过WebService上传文件: 思路是从本地读取一个文件的,得到这个文件的字节数组,然后把字节数组传给到Web Service的方法,如果是多文件上传的话,Web Service接受的方法应该变为一个List<byte []>,在客户端更新Web Service后调用这个方法的时候,发现这个参数的类型变成了一个 ????????? <httpRuntime executionTimeout="300" maxRequestLength="40960" useFullyQualifiedRedirectUrl="false"/>,把这代码添加到<System.Web>节点下面即????????? 可,maxRequestLength可以按照你的需要变化,但是千万不到吧这个数写的太大,那样的话即便在Web Service Build的时候不报错也会在在客户端引用的时候就会报错. 2. FileStream会 根据文件类型进行自动转化,当我做到这的时候陷入了一个误区,在文件格式转换的问题上绕圈
? static public ArrayOfBase64Binary SendFile(string[] fPaths) { FileStream fStream; ArrayOfBase64Binary lstB = new ArrayOfBase64Binary(); int fCount = fPaths.Length; if (fCount != 0) { for (int i = 0; i < fCount; i++) { fStream = new FileStream(fPaths[i],FileMode.Open,FileAccess.Read); Byte[] b = new Byte[fStream.Length]; fStream.Read(b,b.Length); lstB.Add(b); fStream.Flush(); fStream.Close(); } } return lstB;
Web Service端代码: [WebMethod] public void GetFileNew(List<Byte[]> lstB) { GetFiles.AcceptFile(lstB); }
static public void AcceptFile(List<Byte[]> lstB) { for (int i = 0; i < lstB.Count; i++) { string path = "C:Down" + i + ".jpg"; FileStream fStream = new FileStream(path,FileMode.Create,FileAccess.Write); fStream.Write(lstB[i],lstB[i].Length); fStream.Close(); } }
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |