在ASP.NET中使用GridView的多文件上传 [英] Multiple File Uploading using Gridview in ASP.NET

查看:439
本文介绍了在ASP.NET中使用GridView的多文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所试图做的是,我得到的文件名/路径使用asp.net上传控件,然后保存它的路径它的网格视图。例如

What i am trying to do is, i am getting file name/path using asp.net uploader control and then saving its path it grid view. e.g

String path = String.Empty;  
path = FileUploader.FileName;

然后保存在网格视图列这条道路。

and then saving this path in grid view column.

savefiletoGrid(path);

上传的所有必需的文件后,我节省了服务器上的这些文件。像这样

After uploading all required files i am saving these file on server. like this

while( // condition )
{
            string tempfilename = "";  // file name/path from gridview
            string path2 = Server.MapPath("Dir\\" + tempfilename);
            FileUploader.SaveAs(path2);
}

不过,问题是,文件被保存在服务器上使用正确的名称,但与大小为0字节。 请让我知道如何解决这个问题呢?

But, problem is that file is being saved on server with correct name but with size 0 byte. Please let me know how to solve this issue ?

其实我想是这样的客户端上传在asp.net中,我会上传多个文件,并显示在GridView控件(或别的东西),这样用户就可以看到被选择的文件,可以从列出的文件中删除。

Actually i want something like client upload in asp.net, i 'll upload more than one file and show them in gridview ( or in something else ) so that user can see files to be selected and can delete from listed files.

文件'会被保存到服务器上,只有当用户点击一些其他的按钮说'更新'。请你帮帮我,如何做到这一点?

File 'll be saved to server only when user click some other button say 'Update'. could you please help me , how to accomplish this ?

推荐答案

问题已解决通过创建与一列,例如一个DataTable控制(与列数据类型文件上传):

Problem has been solved by creating a DataTable with one column for control (with column data Type FileUpload) for example :

private DataTable CreateDtDocs(string name, string path, FileUpload FileUploader)
    {
        DataTable dt1 = new DataTable();
        dt1.Columns.Add("SR_NO");
        dt1.Columns.Add("Name");
        dt1.Columns.Add("Path");

        Type col_type = fubrowse.GetType();
        DataColumn dt_col = new DataColumn("Control", col_type);

        dt1.Columns.Add(dt_col);

        DataRow dr = dt1.NewRow();
        dr["SR_NO"] = "1";
        dr["NAME"] = name;
        dr["Path"] = path;
        dr["Control"] = FileUploader;
        dt1.Rows.Add(dr);
        return dt1;
    }

然后填写表格如下图所示:

And then Populate table like below :

 private DataTable AddDtDocs(string name, string path, FileUpload FileUploader)
    {
        DataTable dt1 = (DataTable)Session["AttachFilesdt"];
        int count = dt1.Rows.Count;

        DataRow dr = dt1.NewRow();
        dr["SR_NO"] = count + 1;
        dr["NAME"] = name;
        dr["Path"] = path;
        dr["Control"] = FileUploader;
        dt1.Rows.Add(dr);
        return dt1;
    }

然后我加入路径名和控制词典并将它们传递给不同的功能将它们保存在服务器上。

And then i am adding path name and control in Dictionary and passing them to a different function to save them on server.

Dictionary<string, FileUpload> DocsPathAndControl = new Dictionary<string, FileUpload>();
if (Session["AttachFilesdt"] != null)
        {
            tempdt = (DataTable)Session["AttachFilesdt"];
            for (int i = 0; i < tempdt.Rows.Count; i++)
            {
                DocsPathAndControl.Add(tempdt.Rows[i]["Path"].ToString(), (FileUpload)tempdt.Rows[i]["Control"]);
            }
            Session["AttachFilesdt"] = null;
        }

功能来保存文件

Function to save Files

  private void AddDocuments(int jurisdictionID, Dictionary<string,FileUpload> docPathsAndControl)
    {
         foreach (var item in docPathsAndControl)
         {
             string tempfilename = jurisdictionID + "_" + item.Key.ToString();
             string path = Server.MapPath("Dir\\" + tempfilename);
             FileUpload FileUploaderControl = (FileUpload)item.Value;
             FileUploaderControl.PostedFile.SaveAs(path);

          }
    }

希望,这是梦想成真。

Hope, it 'll help.

这篇关于在ASP.NET中使用GridView的多文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆