WebService端:
/// <summary> /// 上传文件到远程服务器 /// </summary> /// <param name="fileBytes"></param> /// <param name="fileName"></param> /// <returns></returns> [WebMethod(Description = "上传文件到远程服务器.")] public string UploadFile(byte[] fileBytes, string fileName) { try { MemoryStream memoryStream = new MemoryStream(fileBytes); //1.定义并实例化一个内存流,以存放提交上来的字节数组。 FileStream fileUpload = new FileStream(Server.MapPath(".") + "\\" + fileName, FileMode.Create); ///2.定义实际文件对象,保存上载的文件。 memoryStream.WriteTo(fileUpload); ///3.把内存流里的数据写入物理文件 memoryStream.Close(); fileUpload.Close(); fileUpload = null; memoryStream = null; return "文件已经上传成功"; } catch (Exception ex) { return ex.Message; } }客户端调用:
private void BtnUpLoadImg_Click(object sender, EventArgs e) { ImgService.ImgService uf = new ImgService.ImgService(); FileInfo imgFile = new FileInfo(@"D:\我的图片\拳皇人物\1_110508104158_7.jpg"); byte[] imgByte = new byte[imgFile.Length];//1.初始化用于存放图片的字节数组 System.IO.FileStream imgStream = imgFile.OpenRead();//2.初始化读取图片内容的文件流 imgStream.Read(imgByte, 0, Convert.ToInt32(imgFile.Length));//3.将图片内容通过文件流读取到字节数组 MessageBox.Show(uf.UploadFile(imgByte, "abc" + System.Guid.NewGuid() + ".jpg"));//4.发送到服务器 }