Access to wwwroot - Asp.Net Core MVC working well on local host but not in published app - asp.net-core-mvc

I'm having a lot of trouble trying to get my App to work when
published. Basically, the code is supposed to create a doc from
template using Open XML sdk, then save to wwwroot and then upload to
blob storage.
It's working fine using local host. Have read and tried some stuff re
accessing static files - but nothing seems to work. Any help would be
very much appreciated. Relevant code is below:
[HttpGet]
public IActionResult GenerateDocxBrowser(MemoryStream mem, string filepath, string inputLastName, string inputTitle, string requestID, string dateReceived, string complaintType, string complaintDetails, string nameString, string no, string street, string town, string postcode)
{
var list = _context.Complaints.Where(s => s.ComplaintId.ToString().Contains(requestID)).ToList();
using (mem = new MemoryStream())
{
filepath = #"wwwroot\RequestTemplate.docx";
nameString = list.Select(s => s.NameString).FirstOrDefault();
complaintDetails = list.Select(s => s.Complaint).FirstOrDefault();
street = list.Select(s => s.AddressStreet).FirstOrDefault();
town = list.Select(s => s.AddressTown).FirstOrDefault();
using (WordprocessingDocument document = WordprocessingDocument.Open(filepath,true))
{
document.GetMergeFields("LastName").ReplaceWithText(inputLastName);
document.GetMergeFields("Title").ReplaceWithText(inputTitle);
document.GetMergeFields("ComplaintID").ReplaceWithText(requestID);
document.GetMergeFields("DateReceived").ReplaceWithText(dateReceived);
document.GetMergeFields("ComplaintType").ReplaceWithText(complaintType);
document.GetMergeFields("ComplaintDetails").ReplaceWithText(complaintDetails);
document.GetMergeFields("NameString").ReplaceWithText(nameString);
document.GetMergeFields("AddressLn1").ReplaceWithText(no + " " + street);
document.GetMergeFields("AddressLn2").ReplaceWithText(town + " TAS " + postcode);
document.SaveAs(#"wwwroot\" + requestID + ".docx");
document.MainDocumentPart.Document.Save();
document.Close();
}
}
const string StorageAccountName = "xxx";
const string StorageAccountKey = "xxxxxx";
var storageAccount = new CloudStorageAccount(
new StorageCredentials(StorageAccountName, StorageAccountKey), true);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("tasman/Request Images");
CloudBlockBlob blockBlob = container.GetBlockBlobReference(requestID + ".docx");
blockBlob.UploadFromFileAsync(#"wwwroot\" + requestID + ".docx");
return View();
}

Your .SaveAs() field should be relative, currently its literally saving to wwwroot somewhere on the drive. You can specify the relative path a few different ways - one of them is below:
var saveToFolder = Path.Combine(Environment.CurrentDirectory, $"/wwwroot/{requestID}.docx");

Related

MVC Rotativa.ActionAsPdf generate Unauthorized in pdf

Rotativa.ActionAsPdf is working fine when you have set Anonymous access true in IIS. but when you select window authentication , the Rotativa.ActionAsPdf Generate pdf with error Unauthorized etc.
google a lot could not find any solution ..
here is my solution :::
create a standard user name in your AD (Active directory) and use this user name and password to generate the file.
example
string iFilename = "eForms_" + Id + "_" + DateTime.Now.ToString("dd_mm_yyyy_hh_mm_ss") + ".pdf";
ViewBag.FileName = Server.MapPath("~" + "/Files/" + iFilename);
var iResult = new Rotativa.ActionAsPdf("displayForm", new { Id = Id }) { FileName = iFilename, SaveOnServerPath = Server.MapPath("~" + "/Files/" + iFilename) };
iResult.UserName = "genericuser";
iResult.Password = "password";
return iResult;

Connecting xamarin to woocommerce api

I'm trying to create my first app that will connect to a woocommerce api.
Has anyone any experience in this or can point me in the direction as to how to create a connection to pull in the product list?
Thanks
Since WooCommerce has a REST API, it should be fairly simple to connect using a plain HTTP request, or a library like RestSharp.
There is also a C# client for WooCommerce - I don't know if it plays well with Xamarin, you might need to modify it a bit to get it to build.
var api = new WoocommerceApiClient(StoreUrl, ConsumerKey, ConsumerSecret);
var result = await api.Products.Get();
It's an old post but I had faced a similar issue. I had tries WoocommerceSharp with Xamarin Studio 6.1.1 (mac version); I opened the .sln file, added the missing reference to system.net.http and it worked perfectly.
If you want make it work in PCL you have to use PCLCrypto in WoocommerceApiUrlGenerator.cs , here the updated version:
namespace SharpCommerce.Web
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using PCLCrypto;
internal class WoocommerceApiUrlGenerator
{
private const string SignatureMethod = "HMAC-SHA1";
private const string ApiV3RootEndpoint = "wc-api/v3/";
private readonly string baseURI;
private readonly string consumerKey;
private readonly string consumerSecret;
internal WoocommerceApiUrlGenerator(string storeUrl, string consumerKey, string consumerSecret)
{
if (
string.IsNullOrEmpty(consumerKey) ||
string.IsNullOrEmpty(consumerSecret) ||
string.IsNullOrEmpty(storeUrl))
{
throw new ArgumentException("ConsumerKey, consumerSecret and storeUrl are required");
}
this.consumerKey = consumerKey;
this.consumerSecret = consumerSecret;
// Need 'http://www.example.com' to be 'http://www.example.com/wc-api/v3/'
this.baseURI = String.Format("{0}/{1}", storeUrl.TrimEnd('/'), ApiV3RootEndpoint);
}
internal string GenerateRequestUrl(HttpMethod httpMethod, string apiEndpoint, Dictionary<string, string> parameters = null)
{
parameters = parameters ?? new Dictionary<string, string>();
parameters["oauth_consumer_key"] = this.consumerKey;
// oauth_timestamp = number of seconds since 1/1/1970 00:00:00 GMT
// must be a positive integer
// must be greater than timestamp of previous requests
parameters["oauth_timestamp"] =
Math.Round(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds).ToString(CultureInfo.InvariantCulture);
// oauth_nonce = a unique random string for the timestamp.
// defends against replay attacks
// service provide will know that this request has never been made before.
// Just going to hash the time stamp.
//parameters["oauth_nonce"] = GenerateNonce(parameters["oauth_timestamp"]);
// Create random 32 char alphnumeric to avoid reused nonces
parameters["oauth_nonce"] = GenerateNonce();
// Declare the hashing method your using
parameters["oauth_signature_method"] = SignatureMethod;
//parameters["oauth_version"] = "1.0";
parameters["oauth_signature"] = UpperCaseUrlEncode(this.GenerateSignature(httpMethod, apiEndpoint, parameters));
var sb = new StringBuilder();
foreach (var pair in parameters)
{
sb.AppendFormat("&{0}={1}", SafeUpperCaseUrlEncode(pair.Key), SafeUpperCaseUrlEncode(pair.Value));
}
// Substring removes first '&'
var queryString = sb.ToString().Substring(1);
var url = this.baseURI + apiEndpoint + "?" + queryString;
return url;
}
private string GenerateSignature(HttpMethod httpMethod, string apiEndpoint, Dictionary<string, string> parameters)
{
// 1) Set the HTTP method for the request.
// set through 'method'
//2) Set your base request URI – this is the full request URI without query string parameters – and URL encode according to RFC 3986:
// need 'http://www.example.com/wc-api/v3/orders'
// to become: 'http%3A%2F%2Fwww.example.com%2Fwc-api%2Fv1%2Forders'
var encodedBaseRequestURI = SafeUpperCaseUrlEncode(this.baseURI + apiEndpoint);
// 3) Collect and normalize your query string parameters
// percent(%) characters should be double-encoded (e.g. % becomes %25.
var normalizedParameters = NormalizeParameters(parameters);
// 4) Sort the parameters in byte-order
var orderedNormalizedParameters = normalizedParameters.OrderBy(x => x.Key).ToList();
// 5) Join each parameter with an encoded equals sign (%3D):
//var joinedOrderedNormalizedParameters = orderedNormalizedParameters.ConvertAll(x => x.Key + "%3D" + x.Value);
var joinedOrderedNormalizedParameters = new List<string>();
foreach (var x in orderedNormalizedParameters)
{
joinedOrderedNormalizedParameters.Add(x.Key + "%3D" + x.Value);
}
// 6) Join each parameter key/value pair with an encoded ampersand (%26):
var joinedParameterPairs = String.Join("%26", joinedOrderedNormalizedParameters);
// 7) Form the string to sign by joining the HTTP method, encoded base request URI, and encoded parameter string with an unencoded ampersand symbol (&):
var stringToSign = string.Format("{0}&{1}&{2}", httpMethod.ToString().ToUpper(), encodedBaseRequestURI, joinedParameterPairs);
// 8) Generate the signature using the string to key and your consumer secret key
var preparedStringToSign = Encoding.UTF8.GetBytes(stringToSign);
var secret = this.consumerSecret + "&";
var preparedConsumerKey = Encoding.UTF8.GetBytes(secret);
var signatureHash = Sha1(preparedConsumerKey, preparedStringToSign);
var signatureString = Convert.ToBase64String(signatureHash);
return signatureString;
}
private static byte[] Sha1(byte[] key, byte[] message)
{
var mac = WinRTCrypto.MacAlgorithmProvider.OpenAlgorithm(MacAlgorithm.HmacSha1);
//var keyMaterial = WinRTCrypto.CryptographicBuffer.ConvertStringToBinary(key, Encoding.UTF8);
var cryptoKey = mac.CreateKey(key);
var hash = WinRTCrypto.CryptographicEngine.Sign(cryptoKey, message);
return hash;
//return WinRTCrypto.CryptographicBuffer.CreateFromByteArraymessage);
}
private static Dictionary<string, string> NormalizeParameters(Dictionary<string, string> parameters)
{
var result = new Dictionary<string, string>();
foreach (var pair in parameters)
{
var upperCaseUrlEncodedKey = SafeUpperCaseUrlEncode(pair.Key);
var normalizedKey = upperCaseUrlEncodedKey.Replace("%", "%25");
var upperCaseUrlEncodedValue = SafeUpperCaseUrlEncode(pair.Value);
var normalizedValue = upperCaseUrlEncodedValue.Replace("%", "%25");
result.Add(normalizedKey, normalizedValue);
}
return result;
}
private static string SafeUpperCaseUrlEncode(string stringToEncode)
{
return UpperCaseUrlEncode(System.Net.WebUtility.UrlDecode(stringToEncode));
}
private static string UpperCaseUrlEncode(string stringToEncode)
{
var basicUrlEncodedString = System.Net.WebUtility.UrlEncode(stringToEncode);
if (String.IsNullOrEmpty(basicUrlEncodedString)) return String.Empty;
var upperCaseUrlEncodedString = Regex.Replace(
basicUrlEncodedString,
"(%[0-9a-f][0-9a-f])",
c => c.Value.ToUpper());
return upperCaseUrlEncodedString;
}
private static string GenerateNonce()
{
const string ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var random = new Random();
var nonceString = new StringBuilder();
for (var i = 0; i < 32; i++)
{
nonceString.Append(ValidChars[random.Next(0, ValidChars.Length - 1)]);
}
return nonceString.ToString();
}
}
}
and in WoocommerceApiDriver.cs you will have to replace
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return await client.GetStringAsync(url);
}
by
using (var client = new HttpClient()) // must use to avoid Android freezes after repeated calls
{
Task<HttpResponseMessage> r = client.GetAsync(url);
HttpResponseMessage m = r.Result;
return await m.Content.ReadAsStringAsync();
}
Et voila ! ca marche ;)

CKEditor file upload doesn't work properly with mvc 6

I'm trying to use the built in upload file of CKEditor, it works with my MVC5 project, but it doesn't work with my MVC6 project, the code for uploading the file is correct, I've tested it, and it actually upload the file to the server, but it doesn't populate the form with the URL and image information, here's the code for my MVC5 project that works:
public ActionResult UploadImage(HttpPostedFileBase upload, string CKEditorFuncNum, string CKEditor,
string langCode)
{
string vImagePath = String.Empty;
string vMessage = String.Empty;
string vFilePath = String.Empty;
string vOutput = String.Empty;
try
{
if (upload != null && upload.ContentLength > 0)
{
var vFileName = DateTime.Now.ToString("yyyyMMdd-HHMMssff") + " - " + Path.GetFileName(upload.FileName);
var vFolderPath = Server.MapPath("/Upload/");
if (!Directory.Exists(vFolderPath))
{
Directory.CreateDirectory(vFolderPath);
}
vFilePath = Path.Combine(vFolderPath, vFileName);
upload.SaveAs(vFilePath);
vImagePath = Url.Content("/Upload/" + vFileName);
vMessage = "The file uploaded successfully.";
}
}
catch(Exception e)
{
vMessage = "There was an issue uploading:" + e.Message;
}
vOutput = #"<html><body><script>window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ", \"" + vImagePath + "\", \"" + vMessage + "\");</script></body></html>";
return Content(vOutput);
}
And here is the code for MVC6 project that doesn't work:
public async Task<ActionResult> UploadImage(IFormFile upload, string CKEditorFuncNum, string CKEditor,
string langCode)
{
string vImagePath = String.Empty;
string vMessage = String.Empty;
string vFilePath = String.Empty;
string vOutput = String.Empty;
try
{
if (upload != null && upload.Length > 0)
{
var vFileName = DateTime.Now.ToString("yyyyMMdd-HHMMssff") + " - " + ContentDispositionHeaderValue.Parse(upload.ContentDisposition).FileName.Trim('"');
var vFolderPath = Path.Combine(_environment.WebRootPath, "Files", "ArticleUploads");
if (!Directory.Exists(vFolderPath))
{
Directory.CreateDirectory(vFolderPath);
}
vFilePath = Path.Combine(vFolderPath, vFileName);
await upload.SaveAsAsync(vFilePath);
vImagePath = Url.Content("/Files/ArticleUploads/" + vFileName);
vMessage = "The file uploaded successfully.";
}
}
catch (Exception e)
{
vMessage = "There was an issue uploading:" + e.Message;
}
vOutput = #"<html><body><script>window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ", \"" + vImagePath + "\", \"" + vMessage + "\");</script></body></html>";
return Content(vOutput);
}
And in CKEditor config file I have:
config.filebrowserImageUploadUrl = '/Admin/Article/UploadImage';
I've inspected the variables, and they send the same value, also worth to note that I'm using the same version of CKEditor, so that can't be the problem, I'd appreciate any help on this.
If the file gets uploaded and you don't see the image gets populated, I guess there should be some problem with the way you return your content, since you are returning html, try to specify your content type, like so:
return Content(vOutput, "text/html");
If that didn't solve your problem, you need to provide more information, tell us what exactly you get from this action in JavaScript side.

How to add image dynamically in the jasper reports in java

Hii Guys !!!
I designed a jasper report to export into pdf which contains image that is stored in my local machine.Now As per my need i need to add the image dynamically from the projects classpath .Below I am posting my code.plz guys help me how to add image dynamically ...
File tempFile = File.createTempFile(getClass().getName(), ".pdf");
try {
FileOutputStream fos = new FileOutputStream(tempFile);
try {
ServletOutputStream servletOutputStream = response.getOutputStream();
InputStream reportStream = getServletConfig().getServletContext().getResourceAsStream("jasperpdf.jasper");
try {
String datum1 = request.getParameter("fromdate");
String datum2 = request.getParameter("todate");
SimpleDateFormat sdfSource = new SimpleDateFormat("dd-MM-yyyy");
Date date = sdfSource.parse(datum1);
Date date2 = sdfSource.parse(datum2);
SimpleDateFormat sdfDestination = new SimpleDateFormat("yyyy-MM-dd");
datum1 = sdfDestination.format(date);
System.out.println(datum1);
datum2 = sdfDestination.format(date2);
System.out.println(datum2);
String strQuery = "";
ResultSet rs = null;
conexion conexiondb = new conexion();
conexiondb.Conectar();
strQuery = "Select calldate,src,dst,duration,disposition,cdrcost from cdrcost where date(calldate) between '" + datum1 + "' and '" + datum2 + "'";
rs = conexiondb.Consulta(strQuery);
JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs);
JasperRunManager.runReportToPdfStream(reportStream, fos, new HashMap(), resultSetDataSource);
rs.close();
Is it working when you have provided the relative path of the image? i.e. images/image.jpg You should have a folder named images in your project and inside that there should be the file image.jpg ..
i'm newbie for jasper report, may be this code useful for you
private static JRDesignImage getImage(int x_postion, int y_position, int width, int height,ScaleImageEnum scale_type, HorizontalAlignEnum align_type,
JRDesignExpression expression) {
JRDesignImage image = new JRDesignImage(null);
image.setX(0);
image.setY(8);
image.setWidth(97);
image.setHeight(50);
image.setScaleImage(ScaleImageEnum.RETAIN_SHAPE);
image.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
image.setExpression(expression);
// TODO Auto-generated method stub
return image;
}
then add
band = new JRDesignBand();
band.setHeight(73);
expression = new JRDesignExpression();
expression.setValueClass(java.lang.String.class);
expression.setText("$P{imagePath}");
// jasperDesign.addField();
band.addElement(getImage(0,8,97,50,ScaleImageEnum.RETAIN_SHAPE,HorizontalAlignEnum.LEFT,expression));

Replacing VAR datatype in .NET 2.0

I am using .NET 2.0, need to implement VAR datatype in here:
var doc = XDocument.Parse(result);
var sw = doc.Descendants("viewport").Elements("southwest").SingleOrDefault();
if (sw != null)
{
var lat = (double)sw.Element("lat");
var lng = (double)sw.Element("lng");
// do stuff
}
I used STRING instead
public string getLatLang(string address)
{
string latlang = "";
string url = "http://maps.googleapis.com/maps/api/geocode/xml?address=" + address + "&sensor=false";
System.Net.WebClient client = new System.Net.WebClient();
string result = client.DownloadString(url);
string doc = System.Xml.Linq.XDocument.Parse(result).ToString();
string sw = doc.Descendants("viewport").Elements("southwest").SingleOrDefault();
if (sw != null)
{
string lat = (double)sw.Element("lat");
string lng = (double)sw.Element("lng");
latlang = lat + "," + lang;
// do stuff
}
return latlang;
}
But I get an error :
'string' does not contain a definition for 'Descendants'
Please help me to replace VAR here.
To replace var, research the actual return type of the method and change it to that. For example, XDocument.Parse can be found on MSDN here In the documentation, it states "Creates a new XDocument from a string", therefore, the return type must be XDocument. And if you drill down into one of the method overloads (like this one), you'll see the actual method's signature which confirms that it does indeed return an XDocument.
Also, Visual Studio has intellisense, so if you hover over something you can generally get details about it. Try typing System.Xml.Linq.XDocument.Parse(, When you type the first paren, you should see a popup in Visual Studio that tells you what the return type is for the method you're using. If intellisense is not working, then check to make sure you have a reference to the DLL.
Also note that Visual Studio has what is known as Object Explorer. This will allow you to see the method signatures of each object you're working with which includes the return types. Simply right click on any object or method and select "Go To Definition". Hopefully, the Visual Studio version you're using has this, if not, consider upgrading because it's extremely useful.
public string getLatLang(string address)
{
string latlang = "";
string url = "http://maps.googleapis.com/maps/api/geocode/xml?address=" + address + "&sensor=false";
System.Net.WebClient client = new System.Net.WebClient();
string result = client.DownloadString(url);
XDocument doc = System.Xml.Linq.XDocument.Parse(result);
XElement sw = doc.Descendants("viewport").Elements("southwest").SingleOrDefault();
if (sw != null)
{
string lat = sw.Element("lat").Value;
string lng = sw.Element("lng").Value;
latlang = String.Format("{0},{1}", lat, lng);
// do stuff
}
return latlang;
}
Edit: Please note that this solution will not work in .NET 2.0 without some hacks due to LINQ and redistributing System.Core is against the EULA, so you'll likely have to change XDocument to XmlDocument and figure out how to integrate it with Google's return value. I believe it has a Load method, or LoadXml method, can't remember which one does which.
var in itself isnt a data type it just takes the data type from the other side of the equality operator. read more about it here
therefore replacing every var with string wont help you will have to use the correct data type instead of var if you cannot use a var.
error in your case is originating because you i think on this line string doc = System.Xml.Linq.XDocument.Parse(result).ToString(); when replacing var you should have used XDocument but instead you used a string to accomodate errors used a ToString() function.
Edit:- after the comment
public string getLatLang(string address)
{
string latlang = "";
string url = "http://maps.googleapis.com/maps/api/geocode/xml?address=" + address + "&sensor=false";
System.Net.WebClient client = new System.Net.WebClient();
string result = client.DownloadString(url);
XDocument doc = System.Xml.Linq.XDocument.Parse(result);
XElement sw = doc.Descendants("viewport").Elements("southwest").SingleOrDefault();
if (sw != null)
{
string lat = (double)sw.Element("lat").Value;
string lng = (double)sw.Element("lng").Value;
latlang = lat + "," + lang;
// do stuff
}
return latlang;
}
The var keyword is used to implicitly type variables. You're letting the compiler infer the type of your variables. But under the covers, once compilation occurs, variables are still assigned specific types.
What you've done wrong is call .ToString() on your XDocument. Now you just have one big string and you're attempting to treat it like it's still an XDocument.
Try this instead.
XDocument doc = System.Xml.Linq.XDocument.Parse(result);
XElement = doc.Descendants("viewport").Elements("southwest").SingleOrDefault();
Edit
Finally got in front of a computer with Visual Studio on it. You have a few more problems.
The following will compile in .NET 3.5+.
public string getLatLang(string address)
{
string latlang = "";
string url = "http://maps.googleapis.com/maps/api/geocode/xml?address=" + address + "&sensor=false";
System.Net.WebClient client = new System.Net.WebClient();
string result = client.DownloadString(url);
XDocument doc = System.Xml.Linq.XDocument.Parse(result);
XElement sw = doc.Descendants("viewport").Elements("southwest").SingleOrDefault();
if (sw != null)
{
string lat = sw.Element("lat").ToString();
string lng = sw.Element("lng").ToString();
latlang = lat + "," + lng;
// do stuff
}
return latlang;
}
(Notice that you were later explicitly casting values to double and still defining their types as string).
As for the error you mention in your comment, I'm not sure... the Elements<T>(IEnumerable<T>, XName) member is included in the extensions class of System.Xml.Linq (MSDN), so it should work. Make sure that you have also included the System.Linq directive with your other using directives:
using System.Linq;
As an aside, I actually don't see how a lot of the code you have written will work in .NET 2.0. The System.Xml.Linq and System.Linq namespaces weren't introduced until .NET 3.5. If you're really using .NET 2.0, then you may want to reference this thread for a work-around for the framework version you're using.
Ok ,Now I tried everything possible with .NET 2.0 ,I came up with this solution which I know is very low level coding but is the only possible solution with 2.0 , this works EXCELLENT FOR .NET 2.0 !!
public string getLatLang(string address)
{
string latlang = "";
string url = "http://maps.googleapis.com/maps/api/geocode/xml?address=" + address + "&sensor=false";
System.Net.WebClient client = new System.Net.WebClient();
string result = client.DownloadString(url);
int firstlat = result.IndexOf("<lat>");
int lastlat = result.IndexOf("</lat>");
int firstlng = result.IndexOf("<lng>");
int lastlng = result.IndexOf("</lng>");
string _latitude = result.Substring(firstlat+5, (lastlat-5) - firstlat);
string _longitude = result.Substring(firstlng+5, (lastlng-5) - firstlng);
latlang = _latitude + "," + _longitude;
return latlang;
}

Resources