2017-05-09 49 views
0

我正在使用PrimeNg的文件上傳,我有我的API啓用Windows身份驗證。現在通過使用model.id和URL構造函數生成的URL成爲角度4 primeng上傳URL Windows認證Cookie

http://localhost:5200/api/v1/document/:id/Upload

我也有authGuard應用到我的組件添加餅乾。

Routes.ts

const routes: Routes = [ 
    { 
     path: '', component: HomeComponent, pathMatch: 'full', canActivate: [AuthGuard] 
    }, 
    { path: 'factory', component: FactoryFormComponent, canActivate: [AuthGuard]}, 
    { path: 'factory/:id', component: FactoryFormComponent, canActivate: [AuthGuard] }, 
    { path: 'supplier', component: SupplierFormComponent, canActivate: [AuthGuard] }, 
    { path: 'supplier/:id', component: SupplierFormComponent, canActivate: [AuthGuard] }, 
    { path: 'businessarea', component: BusinessAreaFormComponent, canActivate: [AuthGuard] }, 
    { path: 'businessarea/:id', component: BusinessAreaFormComponent, canActivate: [AuthGuard] }, 
    { path: 'document/:id/Upload', component: EventFormComponent, canActivate: [AuthGuard] } 
]; 

HTML代碼

<div *ngIf="model.id"> 
        <p-fileUpload name={{model.id}} url={{documentUploadUrl}} (onUpload)="onUpload($event)" (onError)="onError($event)" 
            multiple="multiple" accept=".txt" maxFileSize="10000000"> 
         <template pTemplate type="content"> 
          <ul *ngIf="uploadedFiles.length"> 
           <li *ngFor="let file of uploadedFiles">{{file.name}} - {{file.size}} bytes</li> 
          </ul> 
         </template> 
        </p-fileUpload> 
</div> 

packages.json

"@angular/animations": "^4.1.1", 
    "@angular/common": "^4.1.1", 
    "@angular/compiler": "^4.1.1", 
    "@angular/core": "^4.1.1", 
    "@angular/forms": "^4.1.1", 
    "@angular/http": "^4.1.1", 
    "@angular/platform-browser": "^4.1.1", 
    "@angular/platform-browser-dynamic": "^4.1.1", 
    "@angular/router": "^4.0.0", 
    "bootstrap": "3.3.7", 
    "core-js": "^2.4.1", 
    "font-awesome": "^4.6.3", 
    "ng2-completer": "^0.2.2", 
    "primeng": "^4.0.0", 
    "rxjs": "^5.3.0", 
    "zone.js": "0.8.5" 

當我嘗試上傳它給401未經授權的錯誤。任何建議都是值得歡迎的。

編輯:我可以看有沒有設置Cookie發送上傳控制器請求時,但對於其他的網址,我可以看到一個名爲「ASP.NET_SessionId」的cookie。我懷疑fileupload沒有機制爲windowsauth添加cookie。

編輯2:我寫了自己的httpclient,我用它來添加cookie,是否可以通過它來調用它?

httpclient.ts

@Injectable() 
export class HttpClient extends Http { 

    constructor(backend: ConnectionBackend, 
     defaultOptions: RequestOptions) { 
     super(backend, defaultOptions); 
    } 

    get(url: string, options?: RequestOptionsArgs): Observable<any> { 
     return super.get(url, this.AddCustomOptions(options)); 
    } 

    post(url: string, body: any, options?: RequestOptionsArgs): Observable<any> { 
     return super.post(url, body, this.AddCustomOptions(options)); 
    } 

    put(url: string, body: any, options?: RequestOptionsArgs): Observable<any> { 
     return super.put(url, body, this.AddCustomOptions(options)); 
    } 

    private AddCustomOptions(options: RequestOptionsArgs): RequestOptionsArgs { 
     if (options) 
      options.withCredentials = true; 
     else 
      options = new RequestOptions({ 
       withCredentials: true 
      }); 

     return options; 
    } 
} 

回答