下载文件后,Fileupload无法正常工作

时间:2011-08-17 06:22:30

标签: c# file-upload updatepanel downloadfile

web-application,c#.NET 我在Updatepanel中有一个多视图,有三个视图。 在第三个视图中,我正在上传文件及其工作。 然后在第一个视图中我需要下载文件。 我实现了它。 我想在下载功能后再添加一个AsyncFileUpload Control。 问题是上传工作但如果我先下载文件然后尝试上传文件,它不工作(在同一视图中)。 它工作,如果我不下载文件和上传但不工作,如果我下载,然后上传文件。 上传文件的代码如下。

string filename = Path.GetFileName(AsyncFileUpload1.FileName);
                string ext = Path.GetExtension(filename);
                if (ext == ".exe" || ext == ".EXE" || ext == ".dll" || ext == ".DLL" || ext == ".config" || ext == ".CONFIG" || ext == ".com" || ext == ".COM")
                {
                    fName = null;
                    lblStatus.Text = "You cant upload " + ext.ToString() + " Files";
                }
                else
                {
                    string newfilename =  e.filename;
                    string strPath = MapPath("../MsgAttach/") + Path.GetFileName(newfilename);
                    AsyncFileUpload1.SaveAs(strPath);
                }

以下是下载文件的代码。

string filename = hd_file.Value.ToString();
        string filepath = MapPath("../MsgAttach/" + filename);
        if (File.Exists(filepath))
        {
            byte[] buffer;
            using (FileStream fileStream = new FileStream(filepath, FileMode.Open))
            {
                int fileSize = (int)fileStream.Length;
                buffer = new byte[fileSize];
                // Read file into buffer
                fileStream.Read(buffer, 0, (int)fileSize);
            }
            Response.Clear();
            Response.Buffer = true;
            Response.BufferOutput = true;
            Response.ContentType = "application/x-download";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
            Response.CacheControl = "public";
            // writes buffer to OutputStream
            Response.OutputStream.Write(buffer, 0, buffer.Length);
            Response.End();
        }

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,这些天我一直在寻找他有一些问题,但是我希望有一些同样不方便的人可以提供帮助:

linkMass string = <your new page aspx>;
             ScriptManager.RegisterStartupScript (this.Page, this.GetType (), "script1", "window.open ('" + linkMass +' ',' _self '' directories = no, toolbar = no, scrollbars = no, resizable = no, top = 10, left = 10, width = 200, height = 100 '); ", true);

考虑在同一页面上下载运行的值'_self',并下载新的aspx代码。

Response.Clear();
            Response.Buffer = true;
            Response.BufferOutput = true;
            Response.ContentType = "application/x-download";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
            Response.CacheControl = "public";
            // writes buffer to OutputStream
            Response.OutputStream.Write(buffer, 0, buffer.Length);
            Response.End();