2012-10-11 26 views
5

私はインターネットで検索していますが、役に立たない回答は見つかりませんでした。ファイルをダウンロードするダイアログボックスを保存する、ASP.NETサーバーからクライアントにファイルを保存する

私は、サーバーに展開されているASP.NET Webサイトを持っています。 サーバー上のASP.NET Webサイトは、W:/というディレクトリにアクセスできます。 会社のクライアントがWebサイトにアクセスできます。 Webサイトは、ListBoxにW:/ディレクトリのすべてのPDFファイルをリストします。クライアントは、リストボックスからPDFファイルを選択し、その場所を選択することによってローカルPCに保存することができます。

ウェブページにファイルとして保存するようなものです。

解決策を教えてもらえますか?

答えて

0

正しいキーワードは、「File Browser asp.net」で、ソースコードで多くの例が見つかります。ここで

はCodeProjectのからのいずれかです。

http://www.codeproject.com/Articles/301328/ASP-NETUser-Control-File-Browser

+0

はあなたに@Aristosをありがとう! – user1734609

+0

@ user1734609これはあなたが探しているものですか? :) – Aristos

+0

私は記事を読むのが終わりましたが、正確ではありません:)私はListBoxにPDFファイル名を持っています。 W:/ディレクトリのファイルは、別のサーバーにありますが、ドメイン内にあります。リストボックスには、そのディレクトリのすべてのファイル名が一覧表示されます。会社内のクライアントは、Webサイトを開くと、ディレクトリからPDFファイルのリストを取得できます。次に、1つまたは複数を選択し、[名前を付けて保存]をクリックします。自分のPCに保存できるはずです:)その点を理解していますか? :) – user1734609

0

がWドライブから[]バイトでファイルの内容を取得し、ローカルファイルに書き込みます。

byte[] data = File.ReadAllBytes(WDriveFilePath) 

FileStream file = File.Create(HttpContext.Current.Server.MapPath(MyLocalFile)); 

file.Write(data, 0, data.Length); 
file.Close(); 
+0

これはセーブダイアログを提供しません。 – bgmCoder

4

最後に私はそれをここに投稿ASP.NET

からファイルをダウンロードするために名前を付けて保存]ダイアログボックスをプロンプトの記事を見つけただけでなく他の誰かを助け、いくつかの時間を節約することがあります。

String FileName = "FileName.txt"; 
String FilePath = "C:/...."; //Replace this 
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; 
response.ClearContent(); 
response.Clear(); 
response.ContentType = "text/plain"; 
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";"); 
response.TransmitFile(FilePath); 
response.Flush(); 
response.End(); 
+1

彼の記事のためにコリー・マシューズに感謝します。 – user1734609

+0

.zipファイルをダウンロードできますか?どのようなContentTypeを使用するのですか? – alegz

-1

私はこのようなファイルを取得しました。

protected void btnExportFile_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       Thread newThread = new Thread(new ThreadStart(ThreadMethod)); 
       newThread.SetApartmentState(ApartmentState.STA); 
       newThread.Start();  

       // try using threads as you will get a Current thread must be set to single thread apartment (STA) mode before OLE Exception . 


      } 
      catch (Exception ex) 
      { 

      } 

     } 

     static void ThreadMethod() 
     { 
      Stream myStream; 
      SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 
      saveFileDialog1.FilterIndex = 2; 
      saveFileDialog1.RestoreDirectory = true; 

      if (saveFileDialog1.ShowDialog() == DialogResult.OK) 
      { 
       if ((myStream = saveFileDialog1.OpenFile()) != null) 
       { 
        // Code to write the stream goes here. 
        myStream.Close(); 
       } 
      } 
     } 
1

これは、ローカルにファイルを取得するuser1734609のソリューションの拡張です。

は、サーバからクライアントにファイルをダウンロードするには、次の

public void DownloadFile() 
     { 
      String FileName = "201604112318571964-sample2.txt"; 
      String FilePath = AppDomain.CurrentDomain.BaseDirectory + "/App_Data/Uploads/" + FileName; 
      System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; 
      response.ClearContent(); 
      response.Clear(); 
      response.ContentType = "text/plain"; 
      response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";"); 
      response.TransmitFile(FilePath); 
      response.Flush(); 
      response.End(); 


     } 
関連する問題