//c/s 代码
 private void button1_Click(object sender, EventArgs e)
        {

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = "c:\\";//注意这里写路径时要用c:\\而不是c:\
            openFileDialog.Filter = "所有文件|*.*|word|*.doc|word|*.docx|Excel|*.xlsx|Excel|*.xls|图片pdf|*.pdf|图片png|*.png";
            // openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            DataSet ds = new DataSet();
            try
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    FileInfo files = new FileInfo(openFileDialog.FileName);
                    if (Config.Ext.Contains(files.Extension) == false)
                    {
                        MessageBox.Show("不允许上传此类型的文件");
                        return;
                    }

                    // 获得文件大小KB为单位
                    fileLength = Convert.ToInt32( files.Length /1024);
                    Cursor.Current = Cursors.WaitCursor;
                    WebClient webClient = new WebClient();
                    string name = openFileDialog.SafeFileName;
                    string nametemp = openFileDialog.SafeFileName;
                    AttachmentBLL AttachmentBLL = new BLL.AttachmentBLL();
                    if (AttachmentBLL.CheckName(name))
                    {
                        name = DateTime.Now.ToString("yyyyMMddHHmmss") + name;
                    }
                    string uploadpath = Config.uploadpath;
                    string path = uploadpath + name;
                    string file = openFileDialog.FileName;
                    int len = 1024;
                    byte[] ty = new byte[len];
                    ty = webClient.UploadFile(path, "post", file);// path=http://i.cnblogs.com/MyHandler.ashx?name="文件名称"
                    Tempclass temp = new Tempclass();
                    string attachmentpath = Config.attachmentpath;
                    string url = Config.path + name;
                    temp.Id = AttachmentBLL.AddAttachment(url, openFileDialog.SafeFileName,fileLength);

                    temp.Nane = openFileDialog.SafeFileName;

                    infolist.Add(temp);
                    listannex.DataSource = null;
                    listannex.DataSource = infolist;

                    listannex.DisplayMember = "Nane";
                    listannex.ValueMember = "Id";
                    listannex.ClearSelected();
                    filename = filename + nametemp + "; ";
                    Cursor.Current = Cursors.Default;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

  

//一般处理处理程序中的代码

    /// <summary>
    /// MyHandler 的摘要说明
    /// </summary>
    public class MyHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
            HttpPostedFile imgPostFile = context.Request.Files[0];  //一直为null
            string name = context.Request["name"];
            string path = System.Configuration.ConfigurationManager.AppSettings["path"].ToString(); //保存服务器的地址 C:\\dbi\\WebService\\attachment\\
            imgPostFile.SaveAs(path + name);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

  

最新文章

  1. python学习05——字典
  2. Vsftp精解之安装配置及原理(一)
  3. 【算法与数据结构】二叉搜索树的Java实现
  4. JMS + jboss EAP 6.2 示例
  5. Linux(CentOS)系统下设置nginx开机自启动
  6. angular懒加载机制 刷新后无法回退解决方案
  7. java开源网站
  8. web页面显示折叠树菜单笔记
  9. Linux应用环境实战05:在Ubuntu 14.10中借用Windows的字体 (转)
  10. jQuery插件综合应用1
  11. mfc---拖曳文件
  12. hcna(华为)_Telnet篇
  13. Android_support_v4和V7
  14. Fedora : multilib version problems found
  15. How to Start a Business in 10 Days
  16. 自己实现IOC过程
  17. 【三分钟视频教程】iOS开发中 Xcode 报 apple-o linker 错误的#解决方案#
  18. B/S架构中常用弹出方法 (转)
  19. Centos yum安装java jdk1.8
  20. hdu1181 (变形课)简单地dfs

热门文章

  1. p3c安装使用 编码规范扫描 阿里巴巴出品,挺好用的
  2. loadrunner基础学习笔记八-分析场景
  3. js 消息框
  4. tomcat 查看和修改内存
  5. python----特殊闭包
  6. BZOJ1251序列终结者——非旋转treap
  7. BZOJ2351[BeiJing2011]Matrix——二维hash
  8. JavaScript无法运行问题
  9. First Knight UVALive - 4297(优化高斯消元解概率dp)
  10. gym 101064 G.The Declaration of Independence (主席树)