【发布时间】:2010-02-20 13:08:42
【问题描述】:
我有一个 ASP.NET Web 应用程序,我需要将数据从网页放入输出文本文件。
我想让用户能够选择保存文件的文件夹。例如,当用户单击“浏览”按钮时,应该会出现选择文件夹对话框。
是否可以在asp.net web应用程序中实现这样的东西?
【问题讨论】:
我有一个 ASP.NET Web 应用程序,我需要将数据从网页放入输出文本文件。
我想让用户能够选择保存文件的文件夹。例如,当用户单击“浏览”按钮时,应该会出现选择文件夹对话框。
是否可以在asp.net web应用程序中实现这样的东西?
【问题讨论】:
使用<input type="file">,用户只能浏览其计算机上的文件。他无法查看服务器上的文件夹,除非您给他一个列表或树形视图结构,以便他可以从中进行选择。这是构建这样一个树视图的example。
【讨论】:
编辑:
查看您的评论,我认为您的意思是推送到响应流?
protected void lnbDownloadFile_Click(object sender, EventArgs e)
{
String YourFilepath;
System.IO.FileInfo file =
new System.IO.FileInfo(YourFilepath); // full file path on disk
Response.ClearContent(); // neded to clear previous (if any) written content
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "text/plain";
Response.TransmitFile(file.FullName);
Response.End();
}
这应该会在浏览器中显示一个对话框,允许用户选择保存文件的位置。
你想使用 FileUpload 控件
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.aspx
protected void UploadButton_Click(object sender, EventArgs e)
{
// Specify the path on the server to
// save the uploaded file to.
String savePath = @"c:\temp\uploads\";
// Before attempting to perform operations
// on the file, verify that the FileUpload
// control contains a file.
if (FileUpload1.HasFile)
{
// Get the name of the file to upload.
String fileName = FileUpload1.FileName;
// Append the name of the file to upload to the path.
savePath += fileName;
// Call the SaveAs method to save the
// uploaded file to the specified path.
// This example does not perform all
// the necessary error checking.
// If a file with the same name
// already exists in the specified path,
// the uploaded file overwrites it.
FileUpload1.SaveAs(savePath);
// Notify the user of the name of the file
// was saved under.
UploadStatusLabel.Text = "Your file was saved as " + fileName;
}
else
{
// Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload.";
}
}
【讨论】:
此类下载对话框是特定于浏览器的。
看看带有 Response.Write 的通用处理程序,或者甚至更好地为此目的编写一个 Http 处理程序。
【讨论】: