Hi rani,
Check this example. Now please take its reference and correct your code.
For QRCode i have referred below article.
For inserting file path in database i have referred below article.
Namespaces
using System.Configuration;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using QRCoder;
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View();
}
[HttpPost]
public JsonResult Save(string qrcode)
{
string fileName = qrcode + ".png";
string filePath = "~/Uploads/" + fileName;
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeGenerator.QRCode qrCode = qrGenerator.CreateQrCode(qrcode, QRCodeGenerator.ECCLevel.Q);
using (Bitmap bitMap = qrCode.GetGraphic(20))
{
//Save the QRImage in Folder.
bitMap.Save(Server.MapPath(filePath), ImageFormat.Png);
}
// Save the QRImage path in database.
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
string sql = "INSERT INTO Files VALUES(@Name, @Path)";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@Name", fileName);
cmd.Parameters.AddWithValue("@Path", filePath);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
return Json(filePath.Replace("~", ""), JsonRequestBehavior.AllowGet);
}
}
View
<div ng-app="MyApp" ng-controller="MyController">
<input type="text" ng-model="qrcode" name="qrcode" />
<input type="button" value="Save" ng-click="Save()" />
<hr />
<img ng-src="{{QRCodeImage}}" ng-show="IsVisible" alt="" style="height: 150px; width: 150px" />
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', [])
app.controller('MyController', function ($scope, $http) {
$scope.Save = function () {
if (typeof ($scope.qrcode) == "undefined") {
return;
}
$http({
method: 'POST',
url: '/Home/Save/',
params: { qrcode: $scope.qrcode }
}).success(function (data) {
$scope.QRCodeImage = data;
$scope.IsVisible = true;
});
};
});
</script>
Screenshot