locked
AngularJS with Web API: uploading files RRS feed

  • Question

  • User789833883 posted

    Good day,

    I have beed following at tutorial on how to upload files using angularjs using WebAPI from this link https://code.msdn.microsoft.com/AngularJS-with-Web-API-22f62a6e#content It has been quite helpful,

    In the tutorial there are four method

    Task<IEnumerable<PhotoViewModel>> Get();
    Task<PhotoActionResult> Delete(string fileName);
    Task<IEnumerable<PhotoViewModel>> Add(HttpRequestMessage request);
    bool FileExists(string fileName);
    Now i need another method to get image by the image name, i have been trying with no result

    //Task<PhotoActionResult> GetById(string fileName);

    Please i need help on how to get file by id working

    Thanks.

    Saturday, April 18, 2015 7:55 PM

Answers

  • User1644755831 posted

    Hello atplerry01,

    you could add an overload to get method in LocalPhotoManager class

     public async Task<PhotoViewModel> Get(int ID) 
            { 
                PhotoViewModel photo = new PhotoViewModel(); 
     
                DirectoryInfo photoFolder = new DirectoryInfo(this.workingFolder); 
     
                await Task.Factory.StartNew(() => 
                { 
                    photo = photoFolder.EnumerateFiles() 
                                                .Where(fi => new[] { ".jpg", ".bmp", ".png", ".gif", ".tiff" }.Contains(fi.Extension.ToLower()) && fi.id == ID) 
                                                .Select(fi => new PhotoViewModel 
                                                { 
                                                    Name = fi.Name,                                                 
                                                    Created = fi.CreationTime, 
                                                    Modified = fi.LastWriteTime, 
                                                    Size = fi.Length / 1024 //if you want more properties like image extend your model and add the image here
                                                }) 
                                                .FirstOrDefault(); 
                }); 
                                              
                return photo; 
            } 
    

    then in controller get the photo by id

           // GET: api/Photo/ID
            public async Task<IHttpActionResult> Get(int ID) 
            { 
                var results = await photoManager.Get(ID); 
                return Ok(new { photo = results }); 
            } 
    

    Hope this helps,

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 20, 2015 2:47 AM