WCF上传文件

 public ResponseMsg<string> Upload(Stream stream)
        {
            string direcotry = UploadFloder.Substring(0, UploadFloder.LastIndexOf('/'));
            if (!Directory.Exists(direcotry))
                Directory.CreateDirectory(direcotry);
            //DirectoryInfo di = Directory.CreateDirectory("");
            string fileName = r.Next().ToString();
            string fullPath = Path.Combine(UploadFloder, fileName);
            using (FileStream fs = new FileStream(fullPath, FileMode.Create, FileAccess.Write))
            {
                byte[] btyContent;
                long len;
                IncomingWebRequestContext icontext = null; ;
                try
                {
                    icontext = WebOperationContext.Current.IncomingRequest;
                }
                catch { }
                if (icontext != null)
                {
                    len = WebOperationContext.Current.IncomingRequest.ContentLength;
                }
                else
                {
                    len = stream.Length;
                }
                btyContent = new byte[len];
                stream.Read(btyContent, 0, btyContent.Length);
                fs.Write(btyContent, 0, btyContent.Length);
                fs.Flush();
            }
            return new ResponseMsg<string> { Code = OperationResult.Success, Data = fileName, Msg = "上传成功" };
        }

猜你喜欢

转载自blog.csdn.net/qq_35534449/article/details/80582071
WCF