ASP.NET清除HttpContext.Current.Request.Files

时间:2012-08-20 13:58:42

标签: asp.net file upload httpcontext

如何清除HttpContext.Current.Request.Files列表?

回发后,仍然包含文件。

protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack)
            this.SaveImages();
}

private Boolean SaveImages()
    {
        //loop through the files uploaded
        HttpFileCollection _files = HttpContext.Current.Request.Files;
        //Message to the user
        StringBuilder _message = new StringBuilder("Files Uploaded:<br>");
        try
        {
            for (Int32 _iFile = 0; _iFile < _files.Count; _iFile++)
            {
                if (!string.IsNullOrEmpty(_files[_iFile].FileName))
                {
                    // Check to make sure the uploaded file is a jpg or gif
                    HttpPostedFile _postedFile = _files[_iFile];
                    string _fileName = Path.GetFileName(_postedFile.FileName);
                    string _fileExtension = Path.GetExtension(_fileName);

                    //Save File to the proper directory
                    _postedFile.SaveAs(HttpContext.Current.Request.MapPath("files/") + _fileName);
                    _message.Append(_fileName + "<BR>");
                }
            }
            lblFiles.Text = _message.ToString();
            return true;
        }
        catch (Exception Ex)
        {
            lblFiles.Text = Ex.Message;
            //Refill images in control????
            return false;
        }
        finally
        {
            //Clear HttpContext.Current.Request.Files!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        }
    }

0 个答案:

没有答案