How to create file from local machine to online web server? - c#

How to create a file from a local computer directly onto a web server?
I tried but got this error:
URI format not supported.
My code is:
string path = Server.MapPath("~/App_Data/");
string path = "http://www.domainname.com/App_Data/";
string jsondata = new JavaScriptSerializer().Serialize(enquirydet);
System.IO.File.WriteAllText(path + "enq.json", jsondata);
Any way how I can do that?

Related

How can I save image file to web app through web service project?

I have 2 projects: one is a web service project and other is a web app project. I have to upload an image file from web service and save the same image in a folder in web app project. The file path is stored in the database and image itself is stored in a folder in web app project
Below is my code:
try
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
string strFolder= Server.MapPath("./");
int strtPos = strFolder.IndexOf("webservices_ewbms");
string rootPath = strFolder.Substring(0, strtPos);
string pathOfEwbms = rootPath+"ewbms\\InspectionPhotos";
//string path = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName;
//string filePath = "~/InspectionPhotos/" + Path.GetFileName(str + ".jpeg");
//File.WriteAllBytes(Server.MapPath(filePath), imageBytes);
//insertInsPhotoCmd.Parameters.AddWithValue("#Photos", filePath);
File.WriteAllBytes(Server.MapPath(pathOfEwbms), imageBytes);
insertInsPhotoCmd.Parameters.AddWithValue("#Photos", pathOfEwbms);
insertInsPhotoCmd.ExecuteNonQuery();
}
tran.Commit();
status = "true";
response = "{ \"status\":\"" + status + "\",\"data\":\"Inspection Details Added Successfully \"}";
}
I am able to save the file in web service folder, but I am not getting any idea on how to save the image in a folder in web app. The web app project and web service project are present in the same folder. Please help!
Edit: I have managed to create the physical path of the destination folder of web app, but I am getting an error: E:/New folder (2)/ewbms/InspectionPhotos' is a physical path, but a virtual path was expected. How can I get the virtual path to get the image save at required folder.
In your code, the variable pathOfEwbms is not being framed correctly. Already you did apply MapPath, so again during WriteAllBytes no need to call MapPath. The usage of MapPath twice caused this error : ...is a physical path, but a virtual path was expected.

read XML file from one server to another server on the same network

I have hosted my application on a server having IP 12.3.4.56 and my datastore reside on another server having IP 12.3.4.57 in same network.
I want to read XML file from my datastore server to application server.
When I fire "\\12.3.4.57\ABC\DEF\" in run prompt it opens the correct folder on both server. I have given everyone read/write shared access on folder also.
When I try to read file from my application server using below code it throws an error.
string XMLFilePath = "\\12.3.4.57\ABC\DEF\dir.xml";
XmlDocument DirDoc = new XmlDocument();
DirDoc.Load(XMLFilePath);
Error: The username or password is incorrect.
The same error occurred when I try to copy file from my datastore server to application server using below code.
string sourceFile = "\\12.3.4.57\ABD\DEF\Test123 (26).pdf";
string Folder = HttpContext.Current.Server.MapPath("~/SavedPDFs");
string destPDFFile = string.Concat(Folder, "Test123 (26).pdf");
System.IO.File.Copy(sourceFile, destPDFFile, true);
I believe you are forgetting to escape the backslash causing the "permission" problem.
Your code should look like this
string XMLFilePath = #"\\12.3.4.57\ABC\DEF\dir.xml"; // note the leading # sign
XmlDocument DirDoc = new XmlDocument();
DirDoc.Load(XMLFilePath);
string sourceFile = #"\\12.3.4.57\ABD\DEF\Test123 (26).pdf"; // note the leading # sign
string Folder = HttpContext.Current.Server.MapPath("~/SavedPDFs");
string destPDFFile = string.Concat(Folder, "Test123 (26).pdf"); // I assume that the Folder variable will have the frontslash at the end
System.IO.File.Copy(sourceFile, destPDFFile, true);

Creating directory and files in a remote server

I am trying to create directories and upload files to the server, but it won't give me access to. This is my code:
if (FileUploadControl.HasFile)
{
string path = "~/MSImages/";
string mappath = Server.MapPath(path);
if (FileUploadControl.PostedFile.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
{
string extension = FileUploadControl.PostedFile.FileName;
extension = extension.Substring(extension.LastIndexOf('.'));
if (!Directory.Exists(mappath))
Directory.CreateDirectory(mappath);
string filename = imgext + Request.QueryString["id"];
FileUploadControl.SaveAs(mappath + filename + extension);
}
}
It works when done on my local computer, but not when on my host. How to fix this?
This is most likely a permission issue. Your IIS account user doesn't have the appropriate permissions to change the resource on the other machine.
You could change the IIS user to a low-permission domain user that is just granted access to that specific machine's share. That should fix your issue.

Converting a local folder structure to a folder structure on a server

I'm currently playing around with Paths in .Net and have run into some difficultly with regards to replicating a local folder structure passed as a string from a web service to my site running under localhost on IIS Express.
Essentially, our users will select an image within our desktop software, the local path of which will be sent as a property of the image in our web service. So, when my script accesses the web service, it is fed a string such as:
C:\\Users\\axumadmin\\Pictures\\axumImages\\Countries\\Canada\\canadianFlag.jpg
What our users will then do is FTP this folder structure to a specified directory on our server:
ServerRoot\\umbraco\\axumImages\\Countries\\Canada\\canadianFlag.jpg
The main issue I have here is that I cannot seem to modify the path retreived from the web service to only return the directories from axumImages downwards. So in essence, my local path would be converted to:
axumImages\\Countries\\Canada\\canadianFlag.jpg
I have already tried playing with System.IO.Path to convert this path into the format that I wish to be returned but ultimately all I have acheived so far is either retreiving just the image filename:
canadianFlag.jpg
System.IO.Path.GetFileName(image.FileName);
or the parent directory of the image
C:\\Users\\axumadmin\\Pictures\\axumImages\\Countries\\Canada
Therefore my question is, how can I parse the string so that it is only using axumImages and its descendants?
Any help would be greatly appreciated.
use string.Substring
var startIndex = image.FileName.IndexOf("axumImages");
string test = image.FileName.Substring(startIndex, image.FileName.Length-startIndex)
I ended up solving this in the end using string parsing.
string test = image.FileName.Split(new[] { "axumImages" }, StringSplitOptions.None)[1];
In this example, image.Filename is my filename. So if the filename is:
C:\\Users\\axumadmin\\Pictures\\axumImages\\Countries\\Canada\\canadianFlag.jpg
this will return
\\Countries\\Canada\\canadianFlag.jpg
I can then concatenate this into a usable variable below which then amounts to the path I require:
var actualPath = "axumImages" + "test";

FTP c# against Solaris 10 server

I am trying this code to download a file from a Windows machine using C# against a Solaris machine and I receive error 550 - File unavailable.
string fileName = FileName();
string remoteUri = "xxxx";
var webClient = new WebClient();
webClient.Proxy = null;
webClient.Credentials = new NetworkCredential(Settings.Default.FtpUser, Settings.Default.FtpPassword);
webClient.BaseAddress = "ftp://"+Settings.Default.FtpHost;
webClient.DownloadFile(remoteUri, fileName);
I have validated that the URI works when using it in the address line of an Internet Explorer.
The URI looks like this
ftp://10.99.137.99/opt/scripts/overnight/test.txt
The actual location after the login on the Unix side is
/opt/scripts/overnight/test.txt
on the Unix side.
I am able to view the file after entering my user and password. What am I doing wrong? What other steps can I take? Is there an easy way to use more manual ftp?
Here's another interesting article with a different answer:
FtpWebRequest Download File
string remoteUri = "xxxx";
Have you posted the actual code? This is the name of the remote file. It needs to be ftp://10.99.137.99/opt/scripts/overnight/test.txt not xxxx
If it isn't the actual code, can you post the code you are really using?

Categories

Resources