2016-05-03 32 views
2

私は私のウェブサイト上で機能を実装しており、Angular JSを使っています。私はファイルをにエクスポートするサービスを実装しました。サーバー側にあります。クライアント側では、 "エクスポートオプション"を作成して、ユーザーがそのボタンをクリックすると、そのユーザーのファイルをダウンロードしてローカルマシンのダウンロードフォルダに保存することができます。私はこの質問から「tremendows」の答えからアイデアだ:私はダウンロードフォルダに保存されたファイルを開いたときに、しかし、よくやっている how to download file using AngularJS and calling MVC API?Angular JSとSpring MVCでダウンロードファイルを開くことができません

すべてを、表示されたエラーメッセージがありました:「」ファイル拡張子のファイル形式が有効でないため、Excelはファイル 'filename.xlsx'を開くことができません。ファイルが破損していないこと、ファイル拡張子がファイルの形式と一致していることを確認してください。 "

サービスで(ファイルをエクスポートするために)サービスを実行すると問題がクライアント側から発生したと思います私は問題を調査し続け、ファイルパス ".xlsx"から問題が出てくることがわかりました。

ファイルをエクスポートするためのJavaコードは以下のとおりですサーバー側:

@RequestMapping(value = "/exportFile", method = RequestMethod.GET) 
public String exportHRSalaryByAcc(HttpServletRequest request, HttpServletResponse response, 
     @RequestParam(value = "month", defaultValue = "") String month) throws IOException, GeneralSecurityException, InvalidFormatException{ 
    String rs = FALSE; 
    response.addHeader("Access-Control-Allow-Origin", "*"); 
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); 
    //response.setContentType("application/x-felix; charset=us-ascii"); 
    response.setHeader("Content-Transfer-Encoding", "7bit"); 

    String fName = null; 

    Calendar cal = Calendar.getInstance(); 
    int m = 0; 
    try { 
     if(StringUtils.isNotEmpty(String.valueOf(month)) || StringUtils.isNotEmpty(String.valueOf(year))){ 
      if (DateUtils.isValidFormatver2(Integer.parseInt(month), Integer.parseInt(year))) { 
        m = Integer.parseInt(month); 
        fName = "C:\\LOCAL\\FILE_" + "[" + m + "]" + "[" + y + "]" + ".xlsx"; 
        // CREAT FILE 
        rs = eService.runExportDataHRSalay(m, fName); 
      } else { 
       return ErrorMessage.FILE_MESS; 
      } 
     } else { 
      m = cal.get(Calendar.MONTH) + 1; 
      fName = "C:\\LOCAL\\FILE_" + "[" + m + "]" + ".xlsx"; 
      rs = eService.runExportDataHRSalay(m, fName); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 

    } 
    response.setHeader("Content-Disposition", "attachment; filename=\"" + fName); 
    return rs; 
} 

ここでは、クライアント側のコードと私はangularjsを使用しています:

 ..... 
     month = currentTime.getMonth() + 1; 
     var check = confirm("Are you sure to run the data ?");  
        if(check == true){ 
         $http({ 
          url : www.serviceFromServerSide.abc/exportFile, 
          method : 'GET', 
          headers : { 
           'Content-type' : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 
          }, 
          responseType : 'arraybuffer' 
         }).success(function(data, status, headers, config) { 
          var file = new Blob([ data ], { 
           type : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' 

          }); 
          //trick to download store a file having its URL 
          var fileURL = URL.createObjectURL(file); 
          var a   = document.createElement('a'); 
          a.href  = fileURL; 
          console.dir(fileURL); 
          a.target  = '_blank'; 
          a.download = 'FILE__' + '[' + month + ']'+ '.xlsx'; 
          document.body.appendChild(a); 
          a.click(); 
          $("#loadingImage").css("display", "none"); 
          $("#backgroundPopup").fadeOut(400); 
         }).error(function(data, status, headers, config) { 
         }); 
        }else{}.......... 

私はこれを理解するのを手伝ってください。私はこれのために本当に疲れました。皆、ありがとうございました。

答えて

0

私は同じ問題を持っていた、私は基本的に、それが.XLSによって.xlsxの交換固定:

response.addHeader( "コンテンツ・処分"、 "添付ファイル; ファイル名=あるReport.xlsを");

fName = "C:\\LOCAL\\FILE_" + "[" + m + "]" + "[" + y + "]" + ".xlsx"; 

付::

fName = "C:\\LOCAL\\FILE_" + "[" + m + "]" + "[" + y + "]" + ".xls"; 

あなたのコードで

、このコード行を置き換えます

関連する問題