How to upload file outside the wwwroot folder - c#

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.

Related

Run local .html file in same directory as executable in web browser control (visual studio)

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");

Get path in different project in solution asp.net

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.

ASP.NET FileUpload control retrieving server path not local path

I am currently having a problem when uploading a file using a file upload control in asp.net
I have published my page using IIS on a remote server and I am accessing that link on my local computer.
Currently when im trying to upload a file I get an error saying that path not found meaning its looking for the file path that is on my pc, on the server which of course doesn't exist.
How do I get this to get the path from my pc because im trying to copy/saveAs the file from my pc to the server.
My code:
String pathCombine = System.IO.Path.Combine(destinationDirectory, Path.GetFileName(file));
File.Copy(sourcePath, pathCombine);

how to copy file to another server with different user account?

In my project, when user upload a file in the web page which hosted in serverA, the web page should also copy that to serverB. ServerA and serverB is under the same network which admin can access ServerB file from serverA via network drive(//servername/C$/folderpath).
string fileName = "my test.txt";
string sourcePath = #"C:\myFolder";
string targetPath = #"\\vmtest001\C$\myFolder\subFolder";
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
if (!System.IO.Directory.Exists(targetPath)) //error
{
System.IO.Directory.CreateDirectory(targetPath);
}
I have tried to copy that file to ServerB and the error message is
Access to the path '\vmtest001\C$\myFolder\subFolder'is denied.
I see some solutions for that are using a webserivce to move file, could anyone tell me the possibility of that?
Main question: How to copy file between different server?
Add this to your web.config:
<identity impersonate="true" userName="accountname" password="password" />
But this is not recommended.
You can start a service on ServerA, using FileSystemWatcher to monitor the upload files, copy file to serverB in this service。

Transferring files from one PC to another using C#?

I'm using C#. I want to get the files that are on the server PC to my PC. Both PCs are connected through network.
I have given IP address of that PC in the path, but it's not copying the files to my folder. I'm using the following code, but it's not working:
File.Copy(Path.GetFileName(sourceFile), Path.GetDirectoryName(targetpath));
In sourceFile I have given IP address + folder path of the server PC and in the targetpath i have given the path of the folder of my PC to which I want to copy the files.
Wy do you use Path.GetFileName?
This function get only file name, not full path.
The signature of File.Copy(string sourceFileName, string destFileName) means, that you must use full path to both files.
This code works good:
File.Copy(#"\\server\folder$\test.txt", "test.txt");

Categories

Resources