Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. //backhand
  2.  
  3. [Route("user/PostProfileDocumentImage")]
  4. [AllowAnonymous]
  5. public async Task<string> PostProfileDocumentImage()
  6. {
  7. string finalPath = "http://localhost:51680/";
  8. Dictionary<string, object> dict = new Dictionary<string, object>();
  9. try
  10. {
  11. var httpRequest = HttpContext.Current.Request;
  12.  
  13. foreach (string file in httpRequest.Files)
  14. {
  15. HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created);
  16.  
  17. var postedFile = httpRequest.Files[file];
  18. if (postedFile != null && postedFile.ContentLength > 0)
  19. {
  20. int MaxContentLength = 1024 * 1024 * 1; //Size = 1 MB
  21.  
  22. IList<string> AllowedFileExtensions = new List<string> { ".jpg", ".gif", ".png" };
  23. var ext = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.'));
  24. var extension = ext.ToLower();
  25.  
  26. if (!AllowedFileExtensions.Contains(extension))
  27. {
  28. var message = string.Format("Please Upload image of type .jpg,.gif,.png.");
  29.  
  30. dict.Add("error", message);
  31. return dict.ToString();
  32. }
  33. else if (postedFile.ContentLength > MaxContentLength)
  34. {
  35. var message = string.Format("Please Upload a file upto 1 mb.");
  36.  
  37. dict.Add("error", message);
  38. return dict.ToString();
  39. }
  40. else
  41. {
  42. var filePath = HttpContext.Current.Server.MapPath("~/Content/ProfileUpload/" + postedFile.FileName);
  43. finalPath = finalPath + "/Content/ProfileUpload/" + postedFile.FileName;
  44. postedFile.SaveAs(filePath);
  45. }
  46. }
  47.  
  48. var message1 = finalPath;
  49. return message1; ;
  50. }
  51.  
  52. var res = string.Format("Please Upload a image.");
  53. dict.Add("error", res);
  54. return dict.ToString();
  55. }
  56. catch (Exception ex)
  57. {
  58. var res = string.Format(ex.Message);
  59. dict.Add("error", res);
  60. return dict.ToString();
  61. }
  62. }
  63.  
  64.  
  65. //front
  66. //html
  67. <form *ngIf="!checkIfUploaded()" #form = "ngForm" class="groove">
  68.  
  69. <label class="col-md-4 control-label" for="Logo">License photo</label>
  70. <input type="file" name="photo" ng2FileSelect [uploader]="uploader" enctype = "multipart/form-data"/>
  71. <button type="button" class ="btn btn-success btn-s"
  72. (click)="uploader.uploadAll()"
  73. [disabled] ="!uploader.getNotUploadedItems().length">Upload picture</button>
  74.  
  75. </form>
  76.  
  77. // ts
  78. import { Component, OnInit } from '@angular/core';
  79. import {NgForm} from '@angular/forms';
  80. import { Observable } from 'rxjs/internal/Observable';
  81. import {FileUploader,FileSelectDirective } from 'ng2-file-upload/ng2-file-upload';
  82. import {AppUser} from '../models/appUser'
  83. import {ProfileServiceService} from '../profileService/profile-service.service';
  84. import { UserProfile } from '../models/userProfile'
  85.  
  86.  
  87. const URL = 'http://localhost:51680/api/Upload/user/PostProfileDocumentImage';
  88.  
  89. @Component({
  90. selector: 'app-profile',
  91. templateUrl: './profile.component.html',
  92. styleUrls: ['./profile.component.css'],
  93. providers: [ProfileServiceService]
  94. })
  95. export class ProfileComponent implements OnInit {
  96. profile: AppUser
  97.  
  98. public uploader: FileUploader = new FileUploader({url: URL, itemAlias: 'photo'});
  99. url: string;
  100.  
  101. constructor(private profileServiceService: ProfileServiceService) {
  102. this.uploader.onAfterAddingFile = (file) => {file.withCredentials = false;};
  103. this.uploader.onCompleteItem = (item: any, response: any,status: any, headers: any) => {
  104. this.url=JSON.parse(response);
  105. }
  106. }
  107.  
  108. uploadFile: any;
  109.  
  110. ngOnInit() {
  111. this.profileServiceService.getMethodProfile()
  112. .subscribe(
  113. data => {
  114. this.profile = data;
  115. },
  116. error => {
  117. alert(error.error.ModelState[""][0])
  118. });
  119. }
  120.  
  121. onSubmit(appUser: AppUser, form: NgForm) {
  122. appUser.Activated = false;
  123. appUser.PersonalDocument = this.url;
  124. appUser.Id = this.profile.Id;
  125. this.profileServiceService.putMethodProfile(appUser.Id,appUser)
  126. .subscribe(
  127. data => {
  128. alert("Your changes updated successfully!");
  129. },
  130. error => {
  131. alert(error.error.ModelState[""][0])
  132. });
  133. }
  134.  
  135. onSubmitPassword(changePassword: UserProfile, form: NgForm) {
  136. this.profileServiceService.postMethodProfile(changePassword)
  137. .subscribe(
  138. data => {
  139. alert("Your changes updated successfully!");
  140. form.reset();
  141. },
  142. error => {
  143. alert(error.error.ModelState[""][0])
  144. });
  145. }
  146.  
  147. checkIfUploaded(){
  148. if(this.profile.PersonalDocument == null || this.profile.PersonalDocument == ""){
  149. return false;
  150. }
  151. else{
  152. return true;
  153. }
  154. }
  155.  
  156. handleUpload(data): void{
  157. if(data && data.response){
  158. data = JSON.parse(data.response);
  159. this.uploadFile = data;
  160. }
  161. }
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement