Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //backhand
- [Route("user/PostProfileDocumentImage")]
- [AllowAnonymous]
- public async Task<string> PostProfileDocumentImage()
- {
- string finalPath = "http://localhost:51680/";
- Dictionary<string, object> dict = new Dictionary<string, object>();
- try
- {
- var httpRequest = HttpContext.Current.Request;
- foreach (string file in httpRequest.Files)
- {
- HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created);
- var postedFile = httpRequest.Files[file];
- if (postedFile != null && postedFile.ContentLength > 0)
- {
- int MaxContentLength = 1024 * 1024 * 1; //Size = 1 MB
- IList<string> AllowedFileExtensions = new List<string> { ".jpg", ".gif", ".png" };
- var ext = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.'));
- var extension = ext.ToLower();
- if (!AllowedFileExtensions.Contains(extension))
- {
- var message = string.Format("Please Upload image of type .jpg,.gif,.png.");
- dict.Add("error", message);
- return dict.ToString();
- }
- else if (postedFile.ContentLength > MaxContentLength)
- {
- var message = string.Format("Please Upload a file upto 1 mb.");
- dict.Add("error", message);
- return dict.ToString();
- }
- else
- {
- var filePath = HttpContext.Current.Server.MapPath("~/Content/ProfileUpload/" + postedFile.FileName);
- finalPath = finalPath + "/Content/ProfileUpload/" + postedFile.FileName;
- postedFile.SaveAs(filePath);
- }
- }
- var message1 = finalPath;
- return message1; ;
- }
- var res = string.Format("Please Upload a image.");
- dict.Add("error", res);
- return dict.ToString();
- }
- catch (Exception ex)
- {
- var res = string.Format(ex.Message);
- dict.Add("error", res);
- return dict.ToString();
- }
- }
- //front
- //html
- <form *ngIf="!checkIfUploaded()" #form = "ngForm" class="groove">
- <label class="col-md-4 control-label" for="Logo">License photo</label>
- <input type="file" name="photo" ng2FileSelect [uploader]="uploader" enctype = "multipart/form-data"/>
- <button type="button" class ="btn btn-success btn-s"
- (click)="uploader.uploadAll()"
- [disabled] ="!uploader.getNotUploadedItems().length">Upload picture</button>
- </form>
- // ts
- import { Component, OnInit } from '@angular/core';
- import {NgForm} from '@angular/forms';
- import { Observable } from 'rxjs/internal/Observable';
- import {FileUploader,FileSelectDirective } from 'ng2-file-upload/ng2-file-upload';
- import {AppUser} from '../models/appUser'
- import {ProfileServiceService} from '../profileService/profile-service.service';
- import { UserProfile } from '../models/userProfile'
- const URL = 'http://localhost:51680/api/Upload/user/PostProfileDocumentImage';
- @Component({
- selector: 'app-profile',
- templateUrl: './profile.component.html',
- styleUrls: ['./profile.component.css'],
- providers: [ProfileServiceService]
- })
- export class ProfileComponent implements OnInit {
- profile: AppUser
- public uploader: FileUploader = new FileUploader({url: URL, itemAlias: 'photo'});
- url: string;
- constructor(private profileServiceService: ProfileServiceService) {
- this.uploader.onAfterAddingFile = (file) => {file.withCredentials = false;};
- this.uploader.onCompleteItem = (item: any, response: any,status: any, headers: any) => {
- this.url=JSON.parse(response);
- }
- }
- uploadFile: any;
- ngOnInit() {
- this.profileServiceService.getMethodProfile()
- .subscribe(
- data => {
- this.profile = data;
- },
- error => {
- alert(error.error.ModelState[""][0])
- });
- }
- onSubmit(appUser: AppUser, form: NgForm) {
- appUser.Activated = false;
- appUser.PersonalDocument = this.url;
- appUser.Id = this.profile.Id;
- this.profileServiceService.putMethodProfile(appUser.Id,appUser)
- .subscribe(
- data => {
- alert("Your changes updated successfully!");
- },
- error => {
- alert(error.error.ModelState[""][0])
- });
- }
- onSubmitPassword(changePassword: UserProfile, form: NgForm) {
- this.profileServiceService.postMethodProfile(changePassword)
- .subscribe(
- data => {
- alert("Your changes updated successfully!");
- form.reset();
- },
- error => {
- alert(error.error.ModelState[""][0])
- });
- }
- checkIfUploaded(){
- if(this.profile.PersonalDocument == null || this.profile.PersonalDocument == ""){
- return false;
- }
- else{
- return true;
- }
- }
- handleUpload(data): void{
- if(data && data.response){
- data = JSON.parse(data.response);
- this.uploadFile = data;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement