如何在回发ASP.NET C#时维护文件上传控制 [英] How I maintain file upload control when it is postback ASP.NET C#

查看:47
本文介绍了如何在回发ASP.NET C#时维护文件上传控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加包含一些文本框和文件上传控件的动态gridview。我已经在gridview中完成了动态添加行但是我在fileupload控件中选择了一个文件,当它在新行添加时发回帖子,文件上传控件丢失了文件。我如何坚持文件。

请帮助



我尝试过:



我试过





TextBox box1 =(TextBox)GridView1.Rows [rowIndex] .Cells [1] .FindControl(name);

TextBox box2 =(TextBox)GridView1.Rows [rowIndex] .Cells [2] .FindControl(dob);

TextBox box3 =(TextBox)GridView1.Rows [rowIndex] .Cells [3] .FindControl(aadhar);

DropDownList ddl =(DropDownList)GridView1.Rows [rowIndex] .Cells [ 4] .FindControl(ddl_R1);

FileUpload fu =(FileUpload)GridView1.Rows [rowIndex] .Cells [5] .FindControl(FileUpload2);

图像img =(图像)GridView1.Rows [rowIndex] .Cells [6] .FindControl(pp);

if(fu.HasFile)

{

会话[FileUpload1] = fu;

}

box1.Text = dt.Rows [rowIndex] [Name]。ToString();

box2.Text = dt.Rows [rowIndex] [DOB]。ToString();

box3.Text = dt.Rows [rowIndex] [AadharNo]。ToString();

ddl.SelectedItem.Text = dt.Rows [rowIndex] [Relation ] .ToString();

img.ImageUrl =../ClientPhoto /+ Session [filename];

fu =(FileUpload)(Session [ FileUpload1]);

rowIndex ++;

I'm trying to add dynamic gridview which contains some textboxes and a file upload control. I have completed dynamic addition of row in gridview but i select a file in fileupload control when it post post back during new row addition, file upload control lost the file. How i persist the file.
Please help

What I have tried:

I tried


TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("name");
TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("dob");
TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("aadhar");
DropDownList ddl = (DropDownList)GridView1.Rows[rowIndex].Cells[4].FindControl("ddl_R1");
FileUpload fu = (FileUpload)GridView1.Rows[rowIndex].Cells[5].FindControl("FileUpload2");
Image img = (Image)GridView1.Rows[rowIndex].Cells[6].FindControl("pp");
if (fu.HasFile)
{
Session["FileUpload1"] = fu;
}
box1.Text = dt.Rows[rowIndex]["Name"].ToString();
box2.Text = dt.Rows[rowIndex]["DOB"].ToString();
box3.Text = dt.Rows[rowIndex]["AadharNo"].ToString();
ddl.SelectedItem.Text = dt.Rows[rowIndex]["Relation"].ToString();
img.ImageUrl = "../ClientPhoto/" + Session["filename"];
fu = (FileUpload)(Session["FileUpload1"]);
rowIndex++;

推荐答案

出于安全原因,如果控件保留其文件名,则无法执行此操作意味着您可以设置默认文件名,如果您可以设置默认文件名,那么恶意网站可能会欺骗您上传已知文件。毫无疑问,如果有办法绕过它,那么恶意网站会使用它。相反,您需要重新设计页面的工作方式,这样您就不需要维护文件上载状态。例如,如果回发是添加新的上传控件,那么你可以通过javascript添加新的控件以避免回发,或添加说10上传控件但隐藏9然后而不是添加新的控件,你只需显示一个隐藏的和等等。或者您将进程分成两部分,以便他们执行不涉及文件上载的所有操作,然后在第二阶段执行文件上载。
You can't do this for security reasons, if the control kept its filename then that would mean you could set a default filename, and if you could set a default filename then malicious websites could trick you into uploading known files. There's no point asking if there is a way around this as if there was a way around it then malicious sites would use it. Instead you need to re-architect how your page works such that you don't need to maintain the file upload state. For example if the postback is to add new upload controls then you could add the new controls via javascript to avoid the postback, or add say 10 upload controls but hide 9 and then rather than add new controls you simply show one of the hidden ones and so on. Or you split your process in two so they do everything that doesn't involve file uploads, and then in a second stage do the file uploads.


这篇关于如何在回发ASP.NET C#时维护文件上传控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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