文件上传清除文件 [英] File Uploder Clear File

查看:117
本文介绍了文件上传清除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我使用fileUpload控件选择一个文件,然后单击提交"按钮将文件上传到数据库中.然后将文件上传.但是问题是在上传文件后,如果我使用F5键刷新页面,则会在数据库中再次上传相同的文件.如何防止这种情况? 我正在asp.net中使用C#语言.

First I choose a file using fileUpload control then I clicked a submit button to uploade the file in a database.Then the file is Uploaded. But the problem is after uploading the file if I refresh the page using F5 key the same file is again uploaded in the database.How Can I prevent this? and also How To clear a File which is just uploaded from FileUploader control when submit button is clicked. I am Using C# language in asp.net.

推荐答案

如果可能,您应尽量避免回发下拉列表.否则..将所选文件保存在Session或ViewState中,以便在提交页面时将其取回:
you should try to avoid the postback of the dropdownlist if possible. otherwise.. save the selected file in Session or ViewState to get it back at the time of submitting the page as:

protected void Button1_Click(object sender, EventArgs e)
{
String savePath = @"c:\temp\";
if (FileUpload1.HasFile == true )
{
  String fileName = FileUpload1.FileName.ToString();
  savePath += fileName;
  FileUpload1.SaveAs(savePath);
}
TextBox1.Text=FileUpload1.PostedFile.FileName.ToString();
}


问题归结于浏览器的工作方式.第一次使用文件上传发布页面时,浏览器发送所有数据,包括文件,然后返回HTML进行渲染.如果您点击重新加载(F5),则浏览器将重新发送上一个POST请求,即重新发送文件.

在这种情况下,它会尝试再次上传文件,但是在其他情况下,例如使某人告白或添加一条记录,可能会导致重复.可能会输入两次记录,甚至更糟的是用户向用户收取两次付款.

阻止这种情况的方法是使用一种称为PRG模式的模式. post-redirect-get模式,在http帖子后,而不是显示html,而是重定向回页面(相同或不同).

参考:

文件上传.回传后,hasfile仍然为真!
阻止/禁用参照功能F5键
The problem is due to how browsers work. The first time you POST the page with a file upload, the browser sends all the data, including the file, then returns back HTML to render. If you hit reload (F5) then the browser resends the last POST request meaning resends the file.

In this case it tries to upload a file again, but in other cases such as billign someone or adding a record this can cause duplication. A record could be entered twice or even worse a user charged payment twice.

The way to stop this is to use a pattern called the PRG pattern. The post-redirect-get patter, where after an http post, instead of showing html, you redirect back to a page (either teh same or another).

REF :

file upload .hasfile holds true after post back!!
block / disabled referesh funtion F5 key


这篇关于文件上传清除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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