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();
}
可能是什麼問題?
你能比「不工作」更精確嗎? –
當我第一次下載文件,然後嘗試上傳文件,下載的文件將再次下載... – LeO
除了任何其他錯誤,你忽略了對'FileStream.Read'的調用結果。您絕不應該*假設*對'Stream.Read'的調用將讀取您想要的一次調用中的所有數據。 –