首页 新闻 搜索 专区 学院

winform中listview控件的使用问题

0
[已解决问题] 解决于 2012-03-15 01:33

初学C#,编写一个winform的文件上传,用webclient实现,用listview显示需要上传的文件,并且上传文件有大小限制,超过限制界值的标红,并且不上传。请问如何在上传时,实现标红的文件不上传?请各位大大指教!谢谢!最好有源码。。。

为你而在的主页 为你而在 | 初学一级 | 园豆:200
提问于:2012-03-11 14:38
< >
分享
最佳答案
0

你在上传的时候应该是循环上传的吧,那就判断文件的大小呗,如果大小超过了设置,就跳过,继续上传下一个文件不就可以了。。。

KivenRo | 小虾三级 |园豆:1722 | 2012-03-11 22:08

谢谢。但是出现了个问题,如果我在string path = lvFileList.Items[i].Text;处设断点调试,满足条件的可以完成上传。但是不设断点调试,就会出现漏传。

上传代码如下:

private void btnUpLoadFile_Click(object sender, EventArgs e)
        {
            if (lvFileList.Items.Count <= 0)
            {
                MessageBox.Show("请添加上传文件!");               
            }
            else
            {
                #region  上传实现

                for (int i = 0; i < lvFileList.Items.Count; i++)
                {
                    //lvFileList.Items[i].Checked = true;
                    //lvFileList.Items[i].Selected = true;

                    string path = lvFileList.Items[i].Text;

                    #region 上传文件名称、服务路径设置
                   
                    //是否重命名
                    bool reName = true;
                    //定义文件类型,新的文件名,服务器路径
                    string fileNameExt;
                    string newFileName;
                    string pServerPath;
                    if (reName)
                    {
                        //获取文件扩展名
                        //fileNameExt = filepath.Substring(filepath.LastIndexOf(".") + 1);
                        fileNameExt =path .Substring ( path.LastIndexOf(".") + 1);
                        //定义重命名规则
                        newFileName = DateTime.Now.ToString("yyMMddhhmmss") + "." + fileNameExt;

                    }
                    else
                    {
                        newFileName = filepath.Substring(filepath.LastIndexOf("\\") + 1);
                    }
                    //服务器路径规则
                    if (!txtSavePath.Text.EndsWith("/") && !txtSavePath.Text.EndsWith("\\"))
                    {
                        txtSavePath.Text = txtSavePath.Text + "\\";
                    }

                    //服务器完整保存路径
                    pServerPath = txtSavePath.Text + newFileName;

                    #endregion
                    //实现上传

                    WebClient myWebClient = new WebClient();
                    myWebClient.Credentials = CredentialCache.DefaultCredentials;

                    myfilestream = new FileStream(path, FileMode.Open, FileAccess.Read);
                  
                    //获取文件大小
                    double pSize = Convert.ToDouble(Convert.ToDouble(myfilestream.Length) / Convert.ToDouble(1024));
                   
                    //文件大小控制
                    if (pSize / 1024 <= 10)
                    {
                        BinaryReader br = new BinaryReader(myfilestream);

                        try
                        {

                            byte[] postArray = br.ReadBytes((int)myfilestream.Length);
                            //实例化poststream
                            Stream postStream = myWebClient.OpenWrite(pServerPath, "PUT");
                            if (postStream.CanWrite)
                            {
                                //写出流
                                postStream.Write(postArray, 0, postArray.Length);
                            }
                            else
                            {
                                MessageBox.Show("文件目前不可写!");
                            }

                            postStream.Close();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
                MessageBox.Show("文件上传完成!");

                #endregion
            }
        }

为你而在 | 园豆:200 (初学一级) | 2012-03-12 11:11
其他回答(1)
0

你好在吗?

aaronSuccess | 园豆:202 (菜鸟二级) | 2013-03-28 23:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册