C# 如何从 asp.net Web 应用程序中选择文件夹或文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2302257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:14:42  来源:igfitidea点击:

How could i select folder or file from the asp.net web application?

c#asp.net

提问by Sergey Smelov

I have an ASP.NET web application and I need to put data from the web page to the output text file.

我有一个 ASP.NET Web 应用程序,我需要将网页中的数据放入输出文本文件。

I would like to give user an ability to select the folder where the file will be saved. For example, when the user clicks on the 'Browse' button the select folder dialog should appear.

我想让用户能够选择保存文件的文件夹。例如,当用户单击“浏览”按钮时,应出现选择文件夹对话框。

Is it possible to implement such thing in the asp.net web application?

是否可以在 asp.net web 应用程序中实现这样的东西?

采纳答案by hearn

EDIT:

编辑:

Looking at your comment, I think you mean push to the response stream instead?

看看你的评论,我认为你的意思是推送到响应流?

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

This should display a dialog box in the browser allowing the user to select where to save the file.

这应该会在浏览器中显示一个对话框,允许用户选择保存文件的位置。

You want to use the FileUpload control

您要使用 FileUpload 控件

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.aspx

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.";
    }

  }

回答by Darin Dimitrov

Using <input type="file">the user can only browse the files on his computer. There's no way for him to see folders on the server unless you give him a list or treeview structure so that he can choose from. Here's an exampleof building such a treeview.

使用<input type="file">用户只能浏览他电脑上的文件。他无法看到服务器上的文件夹,除非您给他一个列表或树视图结构,以便他可以从中进行选择。这是构建此类树视图的示例

回答by citronas

Such download dialog is browser specific.

这种下载对话框是特定于浏览器的。

Have a look at generic handlers with Response.Write or even better write a Http Handler for such purpose.

看看带有 Response.Write 的通用处理程序,或者甚至更好地为此目的编写一个 Http 处理程序。