I have 2 projects in my solution, my API project and my project that I send info to API.
this its in my controller on project that sent to API:
pic = Path.GetFileName(file.FileName);//image name
path = Path.GetFullPath(Path.Combine(Server.MapPath("~"), #"WebApi1/Employee/"));//path in my project api
path2 = Path.Combine(path, Path.GetFileName(pic));//try combine path folder in api + image name
file.SaveAs(path2);//try safe image in folder api
using (MemoryStream ms = new MemoryStream())
{
file.InputStream.CopyTo(ms);
byte[] array = ms.GetBuffer();
}
but this says that does not recognize path.
You are trying to access files in another application and Server.MapPath will maps a virtual path to a physical path on the server for the current application.
Server.MapPath("~") returns the physical path to the root of the application
If this two application will be deployed in the same web application it will work on the server, If not you need to create a configration for image folder that specifies the full path you need to use.
Related
In my .net core web API , I'm using OpenHtmlToPdf nuget package for rendering HTML documents to PDF format. The implementation working fine locally but not in the server production K8 environment. I'm getting following message from the logs.
Permission denied
I did several workaround and was able to find out, a lib called OpenHtmltoPdf uses Path.GetTempPath() and Guid.NewGuid() to create a temp file. seems above permission denied error occurs when it trying to access that temp path. this is code snippet from the OpenHtmltoPdf lib. OpenHtmlToPdf git repo
//inside TemporaryPdf class
public static string TemporaryFilePath()
{
return Path.Combine(Path.GetTempPath(), "OpenHtmlToPdf", TemporaryPdf.TemporaryFilename());
}
private static string TemporaryFilename()
{
return Guid.NewGuid().ToString("N") + ".pdf";
}
I just added following line to my application to determine the temp path and its returns File Path is: /tmp/.
Console.WriteLine("File path is:" + Path.GetTempPath());
But in the Kubernetes pods have rwx permissions for /tmp/ folder for the all users.
My problem is, after deploying the API, do the NuGet packages use a separate temp path? So how exactly identify it?
I can't seem to run a local .html file on web browser control that's in the same path as the application directory.
I am getting the current application directory, and adding my file name (such as "index.html") at the end of it however it doesn't build successfully.
What's wrong with my code?
string applicationDirectory = AppDomain.CurrentDomain.BaseDirectory
string myFile = Path.Combine(applicationDirectory, "/Media/index.html");
webBrowser1.Url = new Uri("file:///" + myFile);
You don't have to browse with an url , but with a local file path :
WebBrowser1.Navigate("../Media/index.html");
I am developing a Asp.Net MVC 4 application using VS 2012. The application when running in local is using the IIS Express as web server.I am facing issues in trying to access a file which is part of my solution. I have the following action method :
public FileContentResult GetImage()
{
byte[] imageByte = System.IO.File.ReadAllBytes(#"/MyPics/My.jpg");
string contentType = "image/jpeg";
return File(imageByte, contentType);
}
In the first line, I am getting the following error :
Could not find a part of the path 'C:\Program Files (x86)\IIS Express\~\MyPics\My.jpg'
I know that the above path is not correct but I am not able to understand what path I should provide in order to solve this problem.
Regards
Pawan Mishra
You can use Server.MapPath() to get the actual directory like this:
byte[] imageByte = System.IO.File.ReadAllBytes(Server.MapPath("~/MyPics/My.jpg"));
Some people advocate using HostingEnvironment.MapPath() instead: What is the difference between Server.MapPath and HostingEnvironment.MapPath?
I have mvc 3 application to upload the file using third party plugins. I have following path for uploading the file
String path = Server.MapPath("~/App_Data/Uploads/") + categorypath;
It works fine for the above path ~/App_Data/Uploads/ but I want to upload file to the path D drive as follows
String path = Server.MapPath("D:/Uploads/") + categorypath;
But it throws the following exception at that code:
'd:/Uploads/' is a physical path, but a virtual path was expected.
How can I upload file to the D drive.
i am trying to create a folder and copy some images into it using c# wpf application.
curName = txt_PoemName.Text;
// Specify a "currently active folder"
string activeDir = #"C:\Program Files\Default Company Name\Setup2\Poems";
//Create a new subfolder under the current active folder
string newPath = System.IO.Path.Combine(activeDir, curName);
// Create the subfolder
System.IO.Directory.CreateDirectory(newPath);
foreach(DictionaryEntry entry in curPoem){
string newFilePath = System.IO.Path.Combine(newPath, entry.Key.ToString() + Path.GetExtension(entry.Value.ToString()));
System.IO.File.Copy(entry.Value.ToString(), newFilePath, true);
}
i have successfully created the folder and images. and also i can access them via application. but i cant see them in the location on my local disk. when i restart the machine , then application also cant see them.how can i solve this?
Sounds like you have encountered UAC Data Redirection
http://blogs.windows.com/windows/b/developers/archive/2009/08/04/user-account-control-data-redirection.aspx
You need to either force the application to run as an administrator.
How do I force my .NET application to run as administrator?
Or not save your data in a sensitive area. I would recommend saving in a subfolder of
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);