web上传下载文件
WebService代码:
/// /// 上传文件 /// /// 文件的byte[] /// 上传文件的路径 /// 上传文件名字 /// [WebMethod] public bool UploadFile(byte[] fs, string path, string fileName) { bool flag = false; try { //获取上传案例图片路径 path = Server.MapPath(path); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //定义并实例化一个内存流,以存放提交上来的字节数组。 MemoryStream m = new MemoryStream(fs); //定义实际文件对象,保存上载的文件。 FileStream f = new FileStream(path + "\" + fileName, FileMode.Create); //把内内存里的数据写入物理文件 m.WriteTo(f); m.Close(); f.Close(); f = null; m = null; flag = true; } catch (Exception ex) { flag = false; } return flag; }
[WebMethod(Description = "下载服务器站点文件,传递文件相对路径")] public byte[] DownloadFile(string strFilePath, string path) { FileStream fs = null; string CurrentUploadFolderPath = HttpContext.Current.Server.MapPath(path);
string CurrentUploadFilePath = CurrentUploadFolderPath + "\" + strFilePath; if (File.Exists(CurrentUploadFilePath)) { try { ///打开现有文件以进行读取。 fs = File.OpenRead(CurrentUploadFilePath); int b1; System.IO.MemoryStream tempStream = new System.IO.MemoryStream(); while ((b1 = fs.ReadByte()) != -1) { tempStream.WriteByte(((byte)b1)); } return tempStream.ToArray(); } catch (Exception ex) { return new byte[0]; } finally { fs.Close(); } } else { return new byte[0]; }
客户端代码:
/// /// 上传图片附件 /// /// private bool UploadImage() { bool flag = true; string path = @"C:\Documents and Settings\Administrator\My Documents\My Pictures\10121312156cf4a761c504fe69.jpg";//本地路径 byte[] bytes = GetBytesByPath(path);//获取文件byte[] string uploadPath = "image";//上传服务器文件夹路径 string fileName = "img18.jpg";//文件名称 try { localhost.Service s = new WindowsFormsApplication1.localhost.Service(); if (s.UploadFile(bytes, uploadPath, fileName)) { flag = true; } else { flag = false; } } catch { flag = false; } return flag; } /// /// 根据文件的路径获取图片的byte[] /// /// /// public static byte[] GetBytesByPath(string path) { FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); byte[] bytes = br.ReadBytes((int)fs.Length); fs.Flush(); fs.Close(); return bytes; }
private void DownFile() { localhost.Service s = new WindowsFormsApplication1.localhost.Service(); byte[] bs = s.DownloadFile("img18.jpg","image"); FileStream stream=new FileStream(@"C:\Documents and Settings\Administrator\My Documents\My Pictures\18.jpg", FileMode.CreateNew); stream.Write(bs,0,bs.Length); stream.Flush(); stream.Close(); }
web上传下载文件的更多相关文章
- java web service 上传下载文件
1.新建动态web工程youmeFileServer,新建包com,里面新建类FileProgress package com; import java.io.FileInputStream; imp ...
- WEB上传大文件解决方案
众所皆知,web上传大文件,一直是一个痛.上传文件大小限制,页面响应时间超时.这些都是web开发所必须直面的. 本文给出的解决方案是:前端实现数据流分片长传,后面接收完毕后合并文件的思路.下面贴出简易 ...
- C#实现http协议支持上传下载文件的GET、POST请求
C#实现http协议支持上传下载文件的GET.POST请求using System; using System.Collections.Generic; using System.Text; usin ...
- 【WCF】利用WCF实现上传下载文件服务
引言 前段时间,用WCF做了一个小项目,其中涉及到文件的上传下载.出于复习巩固的目的,今天简单梳理了一下,整理出来,下面展示如何一步步实现一个上传下载的WCF服务. 服务端 1.首先新建一个名 ...
- WebSSH画龙点睛之lrzsz上传下载文件
本篇文章没有太多的源码,主要讲一下实现思路和技术原理 当使用Xshell或者SecureCRT终端工具时,我的所有文件传输工作都是通过lrzsz来完成的,主要是因为其简单方便,不需要额外打开sftp之 ...
- WEB上传大文件
众所皆知,web上传大文件,一直是一个痛.上传文件大小限制,页面响应时间超时.这些都是web开发所必须直面的. 本文给出的解决方案是:前端实现数据流分片长传,后面接收完毕后合并文件的思路.下面贴出简易 ...
- web上传大文件(>4G)有什么解决方案?
众所皆知,web上传大文件,一直是一个痛.上传文件大小限制,页面响应时间超时.这些都是web开发所必须直面的. 本文给出的解决方案是:前端实现数据流分片长传,后面接收完毕后合并文件的思路. 实现文件夹 ...
- rz和sz上传下载文件工具lrzsz
######################### rz和sz上传下载文件工具lrzsz ####################################################### ...
- linux上很方便的上传下载文件工具rz和sz
linux上很方便的上传下载文件工具rz和sz(本文适合linux入门的朋友) ##########################################################&l ...
随机推荐
- js 实现数字格式化(货币格式)几种方法
// 方法一 function toThousands(num) { var result = [ ], counter = 0; num = (num || 0).toString().split( ...
- Redis 常用命令学习二:字符串类型命令
1.赋值与取值命令 127.0.0.1:6379> set foo helloredis OK 127.0.0.1:6379> get foo "helloredis" ...
- Tcp问题汇总
一 TCP三次握手 PS:TCP协议中,主动发起请求的一端称为『客户端』,被动连接的一端称为『服务端』.不管是客户端还是服务端,TCP连接建立完后都能发送和接收数据. 起初,服务器和客户端都为CLOS ...
- windows下编译libevent(2.1.8)及使用
一:获取libevent github地址:https://github.com/libevent/libevent/releases 下载2.1.8稳定版 二:编译libevent 我是用的visu ...
- python第五天---集合与format格式化
""" 集合:set 1.由不同元素组成, 2.无序 3.不可变:数字.字符串.元组 不可变类型 """ s = {1, 2, 3, 4, ...
- java. util. concurrent. atomic
一.原子更新基本类型 AtomicInteger AtomicBoolean AtomicLong 二.原子更新数组 AtomicIntegerArray AtomicLongArray Atomic ...
- Foxmail7.2的账号密码的备份与恢复
1:备份: 1.0 找到Foxmail7.2的安装的位置,例如我的就是 D:\Program Files\Foxmail 7.2; 然后在路径下找到Storage文件夹然后备份里边的内容; 邮箱账号备 ...
- 时间格式_java
@Test public void testDate(){ Date date=new Date(); System.out.println(date); /*日期格式*/ DateFormat df ...
- jacascript CSS样式的脚本化(js)操作
前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 引入CSS有3种方式:行间样式,内联样式和外部链接样式. 在实际工作中,我们使用 javascript 操 ...
- liteide
/liteide$ bin/liteide Cannot mix incompatible Qt library (version 0x40806) with this library (versio ...