發問發問
 

已答覆透過程式下載網站上的檔案

  • 2006年3月23日 上午 09:37Johnny Fang 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    請教各位先進

    小弟寫了一支程式
    讓使用者透過這一支程式去下載某些檔案
    設定 Response.ContentType
    並且送出 Response.BinaryWriter 之後
    檔案下載下來都是正確的

    現在的問題是
    當使用者下載檔案時
    檔案的檔名會是負責下載工作的程式檔名
    而不會是原來的檔名

    例如某 Excel 檔 Test.xls
    透過程式 Download.aspx 下載
    則使用者看到的檔名會變成是 Download.xls

    請問是否有方式能夠變更輸出時的檔案名稱?

解答

  • 2006年3月23日 上午 11:10DotJumMVP使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

    請注意看紅色字的地方

    我大概解釋一下我的流程,

    主要是一般傳上來的檔案會經過一個編碼 ,
    Ex: 2006032100001

    在TABLE中會變成
    FileID                       File_Name    File_ContentType
    2006032100001  , abc.rar ,         application/octet-stream

    有一個網頁專門接這種檔案 DownFile.aspx?FileID=2006032100001  

     string strFileID = dsData.Tables["Data"].Rows[0]["File_ID"].ToString();    

         string strSaveName = dsData.Tables["Data"].Rows[0]["File_Name"].ToString(); 
         string strContentType = dsData.Tables["Data"].Rows[0]["File_ContentType"].ToString(); 

         string filename = @"C:\Inetpub\wwwroot\UPLOAD\"+strFileID;
         //Response.Write(strSaveName);
         int intStart = filename.LastIndexOf("\\")+1;
         
         Response.Clear();
          
     
         this.EnableViewState = false;
     //     Response.AddHeader("Accept-Language", "zh-tw");

    // 這邊就是決定他輸出後的檔名
         Response.AddHeader("Content-Disposition","attachment;filename=" + Server.UrlPathEncode(strSaveName)); 


    //     Response.Charset = "BIG5";
         Response.ContentType = strContentType;
         //決定要輸出的檔案位置在實體位置中他是以2006032100001 
         Response.WriteFile(filename);

         Response.Flush();
         Response.Close();
      
         Response.End();

  • 2006年3月24日 上午 06:38DotJumMVP使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

     

    您是說 出現的檔案 如果原來是 123.zip
    結果你輸出他會並成 download.zip 嗎?
    您先做一個小測試一下,看一下如果你先把這邊這個變數strSaveName

      Response.AddHeader("Content-Disposition","attachment;filename=" + Server.UrlPathEncode(strSaveName));

    改為直接打 123.zip

    Response.AddHeader("Content-Disposition","attachment;filename=" + Server.UrlPathEncode("123.zip "));

    您看一下輸出的資料是否還是 download.zip  還是因該為 123.zip

所有回覆

  • 2006年3月23日 上午 11:10DotJumMVP使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

    請注意看紅色字的地方

    我大概解釋一下我的流程,

    主要是一般傳上來的檔案會經過一個編碼 ,
    Ex: 2006032100001

    在TABLE中會變成
    FileID                       File_Name    File_ContentType
    2006032100001  , abc.rar ,         application/octet-stream

    有一個網頁專門接這種檔案 DownFile.aspx?FileID=2006032100001  

     string strFileID = dsData.Tables["Data"].Rows[0]["File_ID"].ToString();    

         string strSaveName = dsData.Tables["Data"].Rows[0]["File_Name"].ToString(); 
         string strContentType = dsData.Tables["Data"].Rows[0]["File_ContentType"].ToString(); 

         string filename = @"C:\Inetpub\wwwroot\UPLOAD\"+strFileID;
         //Response.Write(strSaveName);
         int intStart = filename.LastIndexOf("\\")+1;
         
         Response.Clear();
          
     
         this.EnableViewState = false;
     //     Response.AddHeader("Accept-Language", "zh-tw");

    // 這邊就是決定他輸出後的檔名
         Response.AddHeader("Content-Disposition","attachment;filename=" + Server.UrlPathEncode(strSaveName)); 


    //     Response.Charset = "BIG5";
         Response.ContentType = strContentType;
         //決定要輸出的檔案位置在實體位置中他是以2006032100001 
         Response.WriteFile(filename);

         Response.Flush();
         Response.Close();
      
         Response.End();

  • 2006年3月24日 上午 05:42Johnny Fang 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    感謝

    目前使用您的方法之後

    不會再出現副檔名被記為 .aspx 的情形

    不過主檔名仍然還是為 download

    但是已經暫解小弟的燃眉之急

     

  • 2006年3月24日 上午 06:38DotJumMVP使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆

     

    您是說 出現的檔案 如果原來是 123.zip
    結果你輸出他會並成 download.zip 嗎?
    您先做一個小測試一下,看一下如果你先把這邊這個變數strSaveName

      Response.AddHeader("Content-Disposition","attachment;filename=" + Server.UrlPathEncode(strSaveName));

    改為直接打 123.zip

    Response.AddHeader("Content-Disposition","attachment;filename=" + Server.UrlPathEncode("123.zip "));

    您看一下輸出的資料是否還是 download.zip  還是因該為 123.zip

  • 2006年3月27日 上午 01:25Johnny Fang 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    感謝

    目前程式已經可以正常運作

    檔名也可以用程式控制了