2011-08-17 62 views
0

web應用程序,c#.NET 我在UpdatePanel中有一個多視圖,並有三個視圖。 在第三個視圖中,我正在上傳文件及其工作。 然後在第一個視圖中我需要下載文件。 我實現了它。 我想在下載功能後再添加一個AsyncFileUpload控件。 問題是上傳正在工作,但如果我第一次下載文件,然後嘗試上傳文件,它不工作(在同一視圖中)。 它的工作,如果我不下載文件和上傳,但不工作,如果我下載,然後上傳文件。 上傳文件的代碼如下。下載文件後Fileupload不工作

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

當我第一次下載文件,然後嘗試上傳文件,下載的文件將再次下載... – LeO

+0

除了任何其他錯誤,你忽略了對'FileStream.Read'的調用結果。您絕不應該*假設*對'Stream.Read'的調用將讀取您想要的一次調用中的所有數據。 –

回答

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();