Partage
  • Partager sur Facebook
  • Partager sur Twitter

Get/Upload fichiers

    22 octobre 2018 à 16:41:23

    Je développe une application angular avec une api .Net Core et je souhaite récupérer des images du server, ainsi qu'en uploader, après de nombreuses recherches aucune des solutions que j'ai trouvées sur internet ne fonctionne alors peut-être pouvez-vous m'aider.

    Voici mon service angular :

    import { Injectable } from '@angular/core';
    import { HttpClient, HttpHeaders } from '@angular/common/http';
    import { Observable } from 'rxjs/Rx';
    
    @Injectable()
    export class FileService {
    
      constructor(private http: HttpClient) {
      }
    
      // Method to get a file
      getFile(id: number): Observable<any> {
        const idFile: number = id === null ? 0 : id;
        const requestUrl = 'api/get-image/' + idFile;
        return this.http.get(requestUrl, { responseType: 'blob' });
      }
    
      // Method to upload a picture
      uploadAvatar(fileList: File[], fileName) {
        if (fileList.length > 0) {
          const file: File = fileList[0];
          const formData: FormData = new FormData();
          formData.append('file', file);
          const requestUrl = 'api/upload';
          this.http.post(requestUrl, formData);
        }
      }
    }
    

    Et voici mon controller .NET :

    [Route("api")]
    public class FileController : Controller
    {
    	[HttpPost("upload")]
    	public async Task Upload(IFormFile file)
    	{
    		if (file == null) throw new Exception("File is null");
    		if (file.Length == 0) throw new Exception("File is empty");
    
    		var filePath = Path.Combine(AppConfig.ResourcesPath + file.FileName);
    		using (Stream fileStream = new FileStream(filePath, FileMode.Create))
    		{
    			await file.CopyToAsync(fileStream);
    		}
    	}
    	
    	[HttpGet("get-image/{id}")]
    	[Produces("image/png")]
    	public IActionResult GetFile(int id)
    	{
    		string path = "";
    		if (id == 0)
    		{
    			path = AppConfig.DefaultImgEvent;
    		}
    		else if (id == -1)
    		{
    			path = AppConfig.DefaultImgUser;
    		}
    		else
    		{
    			Picture picture = _pictureDal.GetById(id);
    			path = picture.Path;
    		}
    		var file = Path.Combine(AppConfig.ResourcesPath, path);
    		_logger.LogInformation(LoggingEvents.GetItem, "Get file {0}", file);
    		return PhysicalFile(file, "image/png");
    	}
    }

    Pour le get j'ai une erreur 404 : Failed to load resource: the server responded with a status of 404 (Not Found) [object%20Blob]:1

    Et pour l'upload je ne parviens même pas à rentrer dans le controller



    • Partager sur Facebook
    • Partager sur Twitter

    Get/Upload fichiers

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown