码迷,mamicode.com
首页 > Web开发 > 详细

c/s 给 服务器上传文件(c/s和b/s互传文件)

时间:2014-05-26 16:15:21      阅读:496      评论:0      收藏:0      [点我收藏+]

标签:c   class   blog   code   tar   ext   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//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);
            }
 
        }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//一般处理处理程序中的代码
 
    /// <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;
            }
        }
    }

  

c/s 给 服务器上传文件(c/s和b/s互传文件),布布扣,bubuko.com

c/s 给 服务器上传文件(c/s和b/s互传文件)

标签:c   class   blog   code   tar   ext   

原文地址:http://www.cnblogs.com/wlzhang/p/3746045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!