<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hemant Joshi</title>
    <description>The latest articles on DEV Community by Hemant Joshi (@codingdefined).</description>
    <link>https://dev.to/codingdefined</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F126938%2Fec9c3f5a-be1b-47ab-8f6e-f539f9ac2509.jpeg</url>
      <title>DEV Community: Hemant Joshi</title>
      <link>https://dev.to/codingdefined</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codingdefined"/>
    <language>en</language>
    <item>
      <title>Uploading Image from Angular to ASP.NET Core Web API</title>
      <dc:creator>Hemant Joshi</dc:creator>
      <pubDate>Fri, 19 Jul 2019 05:37:00 +0000</pubDate>
      <link>https://dev.to/codingdefined/uploading-image-from-angular-to-asp-net-core-web-api-4enm</link>
      <guid>https://dev.to/codingdefined/uploading-image-from-angular-to-asp-net-core-web-api-4enm</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiqx70wmia0abskds8pnu.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiqx70wmia0abskds8pnu.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this post we will be discussing about creating an application where you can upload image from angular 8 to ASP.NET Core Web API using ASP.NET Boilerplate. In this we will be going through Back End first i.e. ASP.NET Core part and then we will go through the frontend i.e. Angular.&lt;/p&gt;

&lt;p&gt;I have written a function UploadProfilePicture in UserAppService which will handle the upload of profile pic and then create image on a predefined location. &lt;/p&gt;

&lt;p&gt;The logic behind it very straight forward where we are providing the path where the file will be stored. We check if the directory exists or not, if not then creating the directory. Then we are creating the file stream object and then storing the file in that location.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public async Task&amp;lt;string&amp;gt; UploadProfilePicture([FromForm(Name = "uploadedFile")] IFormFile file, long userId)
{
  if (file == null || file.Length == 0)
    throw new UserFriendlyException("Please select profile picture");

  var folderName = Path.Combine("Resources", "ProfilePics");
  var filePath = Path.Combine(Directory.GetCurrentDirectory(), folderName);

  if (!Directory.Exists(filePath))
  {
    Directory.CreateDirectory(filePath);
  }

  var uniqueFileName = $"{userId}_profilepic.png";
  var dbPath = Path.Combine(folderName, uniqueFileName);

  using (var fileStream = new FileStream(Path.Combine(filePath, uniqueFileName), FileMode.Create))
  {
    await file.CopyToAsync(fileStream);
  }

  return dbPath;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Few things to note here :
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;IFormFile object will come as null if the name attribute of the file input is not same as that of the name of the parameter used in the controller. So you should name then same or use [FromForm(Name = "")] and then assign the name as shown below.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since we are creating a new directory we need to tell ASP.NET Core to serve the static files from that location. For that we need to modify the Configure method of Startup.cs class as shown below :&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.UseStaticFiles(new StaticFileOptions()
{
 FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"Resources")),
 RequestPath = new PathString("/Resources")
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;When using IFormFile, the swagger will give you multiple text boxes like ContentType, ContentDisposition, Headers, Length, Name, FileName etc  instead of file upload control. To change the textboxes to the actual file upload control we need to implement IOperationFilter and then implement the apply method as shown below. The main part is type where we need to define file, since we are clearing all the previous parameters we also need to add the user id parameter.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Swashbuckle.AspNetCore.Swagger;

using Swashbuckle.AspNetCore.SwaggerGen;

namespace LetsDisc.Web.Host.Startup
{
 public class FileUploadOperation : IOperationFilter
 {
  public void Apply(Operation operation, OperationFilterContext context)
  {
   if (operation.OperationId.ToLower().Contains("upload")))
   {
    operation.Parameters.Clear();
    operation.Parameters.Add(new NonBodyParameter
        {
          Name = "uploadedFile",
          In = "formData",
          Description = "Upload File",
          Required = true,
          Type = "file"
        });
    operation.Parameters.Add(new NonBodyParameter
        {
          Name = "userId",
          In = "query",
          Description = "",
          Required = true,
          Type = "long"
        });
    operation.Consumes.Add("multipart/form-data");
   }
  }
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thus we are finished with the backend, now we will go forward with the frontend implementation.&lt;/p&gt;

&lt;h3&gt;
  
  
  HTML
&lt;/h3&gt;

&lt;p&gt;In HTML we will have an input of type file and then we have both change and click function so that user can upload the same image twice.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="col-md-3 profile-image-edit"&amp;gt;
 &amp;lt;label class="hoverable" for="fileInput"&amp;gt;
 ... img tag
  &amp;lt;span class="hover-text"&amp;gt;Choose file&amp;lt;/span&amp;gt;
  &amp;lt;span class="background"&amp;gt;&amp;lt;/span&amp;gt;
 &amp;lt;/label&amp;gt;
 &amp;lt;br /&amp;gt;
 &amp;lt;input #fileInput id="fileInput" type='file' (click)="fileInput.value = null" value="" (change)="onSelectFile($event.target.files)"&amp;gt;
 &amp;lt;button class="btn btn-default" *ngIf="url" (click)="delete()"&amp;gt;delete&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  CSS
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.hoverable {
    position: relative;
    cursor: pointer;
    height: 150px;
    width: 150px;
    border-radius: 50%;
}

    .hoverable .hover-text {
        position: absolute;
        display: none;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        z-index: 2;
    }

    .hoverable .background {
        position: absolute;
        display: none;
        top: 0;
        left: 0;
        bottom: 0;
        right: 0;
        background-color: rgba(255, 255, 255, 0.5);
        pointer-events: none;
        border-radius: 50%;
        z-index: 1;
    }

    .hoverable:hover .hover-text {
        display: block;
    }

    .hoverable:hover .background {
        display: block;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Code Behind
&lt;/h3&gt;

&lt;p&gt;In the code behind we will have a function which has a fileReader ojject to preview the image as well as we will be calling our backend service for uploading the image.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onSelectFile(files: FileList) {

  if (files.length === 0)
    return;

  this.fileToUpload = files.item(0);


  const fileReader: FileReader = new FileReader();
  fileReader.readAsDataURL(this.fileToUpload);

  fileReader.onload = (event: any) =&amp;gt; {
    this.url = event.target.result;
  };

  this.files.push({ data: this.fileToUpload, fileName: this.fileToUpload.name });

  this._userService.uploadProfilePicture(this.files[0], this.user.id)
    .subscribe((result: string) =&amp;gt; {
      this.userDetails.profileImageUrl = result;
  });
}

delete() {
  this.url = null;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faecpsesko1hwk56s6qkz.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faecpsesko1hwk56s6qkz.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GitHub Commit: &lt;a href="https://github.com/codingdefined/LetsDisc/commit/ab7af63ba3cf94c23278fc2fe00d3769672bf506" rel="noopener noreferrer"&gt;https://github.com/codingdefined/LetsDisc/commit/ab7af63ba3cf94c23278fc2fe00d3769672bf506&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Also Published: &lt;a href="https://www.codingdefined.com/2019/07/uploading-image-from-angular-to-aspnet.html" rel="noopener noreferrer"&gt;CodingDefined.com&lt;/a&gt;
&lt;/h2&gt;

</description>
      <category>angular</category>
      <category>aspnetcore</category>
    </item>
    <item>
      <title>How I deployed my Node.js Bot to Heroku</title>
      <dc:creator>Hemant Joshi</dc:creator>
      <pubDate>Mon, 14 Jan 2019 09:28:56 +0000</pubDate>
      <link>https://dev.to/codingdefined/how-i-deployed-my-nodejs-bot-to-heroku-18fc</link>
      <guid>https://dev.to/codingdefined/how-i-deployed-my-nodejs-bot-to-heroku-18fc</guid>
      <description>&lt;p&gt;As you all know that Heroku is a platform as a service which helps developers to build, run, and operate applications entirely in the cloud. I have started with free dyno which is for experimenting and only has 512 MB RAM.&lt;/p&gt;

&lt;p&gt;I am using Windows so the steps will be of Windows machine.&lt;/p&gt;

&lt;p&gt;At first I need to download the Heroku CLI from &lt;a href="https://devcenter.heroku.com/articles/heroku-cli"&gt;https://devcenter.heroku.com/articles/heroku-cli&lt;/a&gt;. It says that it updates the path but you need to double check, for my case I need to manually update the Path. Before installing just check that you have node and npm installed in your system.&lt;/p&gt;

&lt;p&gt;Then I logged into Heroku using command heroku login which asked my Email and Password. Once authenticated, I created my first app using command heroku create which creates a app for me.&lt;/p&gt;

&lt;p&gt;Since my app was not in GitHub, I need to create it using git init and then adding heroku as my remote using command git remote add heroku &lt;a href="https://git.heroku.com/my-app-name.git"&gt;https://git.heroku.com/my-app-name.git&lt;/a&gt;. Then we need to commit the code using command git commit -am "Initial Commit". Once done, just pushing it to heroku git push heroku master.&lt;/p&gt;

&lt;p&gt;Since my app is a bot, I need to create a ProcFile on the root of the directry, the contents of the ProcFile is shown below&lt;/p&gt;

&lt;p&gt;&lt;code&gt;worker: node index.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I pushed the code again with the same command git push heroku master. And then switched on the Worker and switched off the web as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cOSVW4bz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://3.bp.blogspot.com/-Rqfs7aKavsM/W89P4qtJacI/AAAAAAAAC2Y/_E9Bj4vN3-Q6YY4pi3G-Z0kZeg6Pdy3WgCLcBGAs/s640/heroku-deployment.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cOSVW4bz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://3.bp.blogspot.com/-Rqfs7aKavsM/W89P4qtJacI/AAAAAAAAC2Y/_E9Bj4vN3-Q6YY4pi3G-Z0kZeg6Pdy3WgCLcBGAs/s640/heroku-deployment.PNG" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also since I was using some confidential information in my bot, like a bot key I created a config vars in the settings tab as shown below and then used it using process.env.KEY and process.env.NAME in the bot.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gFKDpwo0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://4.bp.blogspot.com/-OSgpIBQ_hqM/W89QFE4olAI/AAAAAAAAC2c/jXa01CnMBdUpmV1t7xG8DtQRtjmJAwNrwCLcBGAs/s1600/heroku-deployment1.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gFKDpwo0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://4.bp.blogspot.com/-OSgpIBQ_hqM/W89QFE4olAI/AAAAAAAAC2c/jXa01CnMBdUpmV1t7xG8DtQRtjmJAwNrwCLcBGAs/s1600/heroku-deployment1.PNG" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this way you can deploy your bot with Heroku. Happy Coding :)&lt;/p&gt;

&lt;p&gt;Source : &lt;a href="https://www.codingdefined.com/2018/10/how-i-deployed-my-nodejs-bot-to-heroku.html"&gt;CodingDefined.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>heroku</category>
      <category>node</category>
      <category>devops</category>
    </item>
    <item>
      <title>How I added AppVeyor, Codecov to .Net Core Open Source Project</title>
      <dc:creator>Hemant Joshi</dc:creator>
      <pubDate>Thu, 10 Jan 2019 10:22:44 +0000</pubDate>
      <link>https://dev.to/codingdefined/how-i-added-appveyor-codecov-to-net-core-open-source-project-54m6</link>
      <guid>https://dev.to/codingdefined/how-i-added-appveyor-codecov-to-net-core-open-source-project-54m6</guid>
      <description>

&lt;p&gt;&lt;a href="https://www.codingdefined.com/2018/10/how-i-added-appveyor-codecov-to-net.html"&gt;Source Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;AppVeyor is Continous Integration and Deployment solution where you can build, test and deploy your apps faster to any platform. One good advantage of using appveyor is that it is free for Open Source repositories. It's actually good when your repo is accepting pull request from people and then you can test if the pull request is merging or not.&lt;/p&gt;

&lt;p&gt;On the other hand, Codec helps you to do a health check on your code, i.e. it is a measurement on how many lines of your code are executed when automated tests are performing. Now 100% Code Coverage means that all your code is covered under tests, but that is very hard to achieve.&lt;/p&gt;

&lt;p&gt;I have to do a lot of commits to test the AppVeyor and Codecov and at last, it iwas successful and I can build and generate a code coverage report for my application. At first, I have started using NuGet package OpenCover to create code-coverage in my local environment and it was working after trial and error method.&lt;/p&gt;

&lt;p&gt;The Script I have used is as below&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;OpenCover.Console.exe -register:user
-target:"C:\Program Files\dotnet\dotnet.exe"
-targetargs:"test\LetsDisc.Tests\bin\Debug\netcoreapp2.1\LetsDisc.Tests.dll -noshadow"
-oldStyle -output:".\coverage.xml"
-filter:"+[LetsDisc*]* -[Tests*]*" -coverbytest:"XUnit"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But as a developer, we should know that what works in local has a 100% probability that it will not work in some other environments. That is what happened in my case when I tried to run the above script in AppVeyor some or the other issues are popping up. But at last, I made it work. So the final script is&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: 1.0.{build}
image: Visual Studio 2017
configuration: Release
skip_tags: true
before_build:
 - nuget restore
 - nuget install OpenCover -OutputDirectory packages -Version 4.6.519
build_script:
 - msbuild /verbosity:quiet LetsDisc.sln
test_script:
 - .\packages\OpenCover.4.6.519\tools\OpenCover.Console.exe
-register:user
-target:"C:/Program Files/dotnet/dotnet.exe"
-targetargs:"test --logger:trx;LogFileName=results.trx
/p:DebugType=full
C:\projects\letsdisc\test\LetsDisc.Tests\LetsDisc.Tests.csproj"
-output:"coverage.xml" -filter:"+[LetsDisc*]* -[Tests*]*"
after_test:
  - ps: |
      $env:PATH = 'C:\msys64\usr\bin;' + $env:PATH
      Invoke-WebRequest -Uri 'https://codecov.io/bash' -OutFile codecov.sh
      bash codecov.sh -f "coverage.xml"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The above script means on every build I will be updating it with a version of 1.0.{buildNumber}, then use Visual Studio 2017 as an image to build the project, then use configuration as release mode and not debug so that it can be easily deployable. Before the build just restoring all the nuget package, as well as installing OpenCover specific version which works with AppVeyor. After that building the project using msbuild and then running my test script to run the tests and create the coverage report. After the test is scuccessful deploying the coverage.xml file to the Codecov website.&lt;/p&gt;

&lt;p&gt;Repo : &lt;a href="https://github.com/codingdefined/LetsDisc"&gt;https://github.com/codingdefined/LetsDisc&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It was fun as well as a learning experiment where I learned to use an Appveyor app as well adding the Coverage report to my project.&lt;/p&gt;


</description>
      <category>opensource</category>
      <category>netcore</category>
    </item>
  </channel>
</rss>
