2017-03-03 11 views
0

herengprime fileuploadを使用してファイルをアップロードし、rest serviceに送信してswagger jsonを検証し、ファイルのアップロード中にファイルコンテンツをdbに保存します。angular2 primengファイルアップロード問題

は、ここに私のfileupload.html

<div class="ui-g"> 
<p-growl [value]="msgs"></p-growl> 
<form [formGroup]="uploadForm" novalidate> 
<div class="ui-g-12"> 
    <div class="ui-grid-row"> 
    <div class="ui-grid-col-12" [ngClass]="{'has-error':!uploadForm.controls['activity'].valid && uploadForm.controls['activity'].touched}"> 
     <div class="ui-grid-col-2"><label>Product Name </label></div> 
     <div class="ui-grid-col-8"> 
     <input class="inputtext" type="text" formControlName="activity" placeholder="Product Activity"/> 
     <div *ngIf="uploadForm.controls['activity'].hasError('required') && uploadForm.controls['activity'].touched" class="alert alert-danger">You must enter Product Name.</div> 
     </div> 
    </div> 
    </div> 
</div> 
<div class="ui-g-12" > 
    <p-fileUpload name="demo[]" url="{{uploadUrl}}" (onUpload)="onUpload($event)" accept=".json,.text,.yml" maxFileSize="1000000" [disabled]="!uploadForm.valid"> 
    <template pTemplate="content"> 
     <ul *ngIf="uploadedFiles.length"> 
     <li *ngFor="let file of uploadedFiles">{{file.name}} - {{file.size}} bytes</li> 
     </ul> 
    </template> 
    </p-fileUpload> 
</div> 

upload.html view

私は取得しています... XMLHttpRequest cannot load http://localhost:8080/upload. Response for preflight is invalid (redirect)エラーです。実際に私がローカルで実行している私の残りのsereviceに行っていない要求...

@RequestMapping(value = "/upload", method = RequestMethod.POST) 
public ResponseEntity uploadFile(MultipartHttpServletRequest request, HttpServletResponse response, HttpServletRequest httpRequest) { 
    /swagger validation logic 
} 

私のコードの問題は何ですか?

+0

基本的にアップロードがMultipartFile MediaType.MULTIPART_FORM_DATA_VALUE – Karthigeyan

+0

するファイル私は同じ 'http.post(URL、ファイルを)やったところそれは' 'Angular1.4'のために働いていた、ありますとにかくjsonやテキストファイルをアップロードするには? – user1653027

答えて

0

「myfileの」ここ

@RequestMapping(value = "/upload", 
    method = RequestMethod.POST, 
    produces = MediaType.APPLICATION_JSON_VALUE, 
    consumes = MediaType.MULTIPART_FORM_DATA_VALUE) 
    public @ResponseBody ResponseEntity uploadFile(@RequestParam("myfile") MultipartFile file, String fileName) throws IllegalStateException, IOException { 
     /swagger validation logic 
     } 

をアップロードされたファイルを消費し、次のRESTエンドポイントを使用してJSONレスポンスを作成するには、コンポーネントに指定された名前の属性を参照します。

+0

私はカスタムアップロードを使用しています。私のuploadHandlerが呼び出されています。しかしその後、エラーはなく、私のコントローラーに電話していません。 UIコードにエラーが発生していないので、何が起こっているのかわからない。 – Sujoy

+0

あなたが提供した情報に基づいて本当に答えが難しいです。問題は何でもかまいません。任意のRESTクライアントを使用してエンドポイントに電話をかけ、ヒットできるかどうか確認してください。または、コードの部分をアップロードするだけです(バックエンドとコードを除く) –

+0

私はあなたに同意します。私は別のスレッドで尋ねなければならないと思う。 – Sujoy

0

まあ、私は以下のジャージーを使用していますですprimengファイルアップロードのための私の作業コード(角4)

HTMLコード

<div class="row header"> 
    <div class="col-xs-1">&nbsp;</div> 
    <div class="col-xs-1">&nbsp;</div> 
    <div class="col-xs-1">&nbsp;</div> 
    <div class="col-xs-1">&nbsp;</div> 
    <div class="col-xs-1">&nbsp;</div> 
    <div class="col-xs-1">&nbsp;</div> 
    <div class="col-xs-1">&nbsp;</div> 
    <div class="col-xs-1">&nbsp;</div> 
    <div class="col-xs-1">&nbsp;</div> 

    <div class="col-xs-3"> 
    <p> 
    owner-shop-customization works! 
    <p-fileUpload name="file" url="http://localhost:1006/jsflab/rest/OwnerMyShop/uploadShopLogo"></p-fileUpload> 
</p> 
    </div> 
</div> 

TS

import { Component, OnInit } from '@angular/core'; 
import {FileUploadModule} from 'primeng/primeng'; 

@Component({ 
    selector: 'app-owner-shop-customization', 
    templateUrl: './owner-shop-customization.component.html', 
    styleUrls: ['./owner-shop-customization.component.css'] 
}) 
export class OwnerShopCustomizationComponent implements OnInit { 

    constructor() { } 

    ngOnInit() { 
    } 

} 

Webサービス

@POST 
    @Path("/uploadShopLogo") 
    @Consumes({MediaType.MULTIPART_FORM_DATA}) 
    public Response uploadPdfFile( @FormDataParam("file") InputStream fileInputStream, 
            @FormDataParam("file") FormDataContentDisposition fileMetaData) throws Exception 
    { 
      String fileLocation = "C://" + fileMetaData.getFileName(); 
        //saving file 
      try { 
       FileOutputStream out = new FileOutputStream(new File(fileLocation)); 
       int read = 0; 
       byte[] bytes = new byte[1024]; 
       out = new FileOutputStream(new File(fileLocation)); 
       while ((read = fileInputStream.read(bytes)) != -1) { 
        out.write(bytes, 0, read); 
       } 
       out.flush(); 
       out.close(); 
      } catch (IOException e) {e.printStackTrace();} 
      String output = "File successfully uploaded to : " + fileLocation; 
      return Response.status(200).entity(output).build(); 
     } 

web.xml

<servlet> 
     <servlet-name>Jersey RESTful Application</servlet-name> 
     <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
     <init-param> 
     <param-name>jersey.config.server.provider.packages</param-name> 
     <param-value>invoicegeneratorwebservice</param-value> 
     </init-param> 
     <init-param> 
    <param-name>jersey.config.server.provider.classnames</param-name> 
    <param-value>org.glassfish.jersey.filter.LoggingFilter; 
    org.glassfish.jersey.media.multipart.MultiPartFeature</param-value> 
</init-param> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Jersey RESTful Application</servlet-name> 
     <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 

のpom.xml

<dependency> 
    <groupId>org.glassfish.jersey.bundles</groupId> 
    <artifactId>jaxrs-ri</artifactId> 
    <version>2.17</version> 
</dependency> 

<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson --> 
<dependency> 
    <groupId>org.glassfish.jersey.media</groupId> 
    <artifactId>jersey-media-json-jackson</artifactId> 
    <version>2.25.1</version> 
</dependency> 
<dependency> 
    <groupId>org.glassfish.jersey.media</groupId> 
    <artifactId>jersey-media-multipart</artifactId> 
    <version>2.17</version> 
</dependency> 
関連する問題