Save files to external(other domain server) server in asp.net - c#

Is it possible to save file to other domain path from file control. I have 2 website www.domain1.com & www.domain2.com. Now I have file control in www.domain1.com & here I want to upload a file which will be store in some directory of www.domain2.com. I don't know is it possible or not. I am trying below basic code but it's not working
if (mainImageUploader.FileName != "")
{
System.IO.FileInfo file = new System.IO.FileInfo(mainImageUploader.PostedFile.FileName);
string newname = file.Name.Remove((file.Name.Length - file.Extension.Length));
newname = (newname + System.DateTime.Now.ToString("_ddMMyyhhmmss")) + file.Extension;
mainImageUploader.SaveAs(Server.MapPath("http://www.domain2.com/images/client-store/" + path + newname));
}

No, not possible if your other server is configured anywhere near correctly.
You need to enable endpoint on your other server to provide file upload. And probably need to secure it somehow - i.e. with username/password.

It's not possible. The better way is creating a WebApi to save file on its server on your second website and save file via calling it from your first website.
Each web server only let to each website to access to its root and subfolders or files.

Related

Azure Website Writing file to a network Drive

I have a service that I currently run on our local IIS machine that will write a file to a specified directory using the following code:
//Creating the filepath that is needed
string FilePath = FolderPath.Replace("'", "")
+ FileName.Replace("'", "").Replace(" ", "")
.ToString();
//Writing the file to the correct path
File.WriteAllBytes(FilePath, fileTransferDump.FileBinary);
When I change over to an Azure Website how would I make the System write to the file path that I would like to write to.
Basically how do I cause the Azure website to write to a network share. This is just a normal Azure Website.
Azure Websites don't have the ability to write to off-machine resources using traditional windows file shares. Your best bet would be to wrap any of this logic in a custom library that redirects these file writes to Azure Blob Storage instead.

A default path when downloading PDF file in computer/mobile phone

I created a PDF webapp where users are able to generate various type of PDF on both the computer and mobile phone. However, i run my program on a localhost and this is how i save my PDF based on my computer's file directory
var output = new FileStream(Path.Combine("C:\\Users\\apr13mpsip\\Downloads", filename), FileMode.Create);
However, when i publish my webapp onto azure, i wasn't able to download from both my computer and mobile phone. Therefore i believe that it could be due to my default file directory.
Hence i would like to ask how to do a default file directory for all computer and mobile phone?
Or could it be i left out something that is necessary when the webapp is published online
Thanks.
PS : I hardcoded a default file path in order for me to test my application on a localhost to ensure a perfect working condition. Therefore i'm finding a way to find a default common file directory for all mobile/computer users when they attempt to download the PDF instead of my usual hard-coded file path
UPDATE
I tried using the method Server.MapPath but receive some error.
var doc1 = new Document();
var filename = Server.MapPath("~/pdf") + "MyTestPDF" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf";
// var output = new FileStream(Path.Combine("C:\\Users\\apr13mpsip\\Downloads", filename), FileMode.Create);
//iTextSharp.text.pdf.PdfWriter.GetInstance(doc1, output);
using (var output = File.Create(filename))
{
iTextSharp.text.pdf.PdfWriter.GetInstance(doc1, output);
}
doc1.Open();
This is the error i received
ObjectDisposedException was unhandled by user code
Cannot access a closed file.
When you write a Web Application you shall never use hard coded paths, and the last place where you should save files is C:\Users !! It does not matter whether this is Azure or not. It is general rule for any kind of web applications!
In your case I suggest that you create a folder within your application named pdf or something like that and save files there with the following code:
var fileName = Server.MapPath("~/pdf") + filename;
using (var output = File.Create(fileName) )
{
// do what you want with that stream
// usually generate the file and send to the end user
}
However there is even more efficient way. Use the Response.OutputStream and write the resulted PDF directly to the response. Will save you a lot of space on the local server, and the logic to delete unused generated files.

Download File via Tamir.SharpSSH.Sftp.Get()

I'm trying to download a file using the Tamir SSH library. I'm able to connect to the remote FTP site, upload files to the site, but I'm getting exceptions when trying to download. I've given IIS_IUSRS full control of the local directory as well as ASPNET. I've tested an I'm able to create a text file in the same local directory I'm trying to download to. Any ideas?
string SFTP_HOST = ConfigurationManager.AppSettings["AccentivFtpHost"];
string SFTP_USERNAME = ConfigurationManager.AppSettings["AccentivFtpUsername"];
string SFTP_PASSWORD = ConfigurationManager.AppSettings["AccentivFtpPassword"];
Sftp client = new Sftp(SFTP_HOST, SFTP_USERNAME, SFTP_PASSWORD);
client.Connect(22);
client.Get("test.txt", "c:\\test.txt");
You probably lack a '/' character in the file directory. You may need to put it either in the Get function call before the "test.txt" like "/test.txt" or at the end of your AccentivFtpHost value in the app config file.

Reading File From Network Location

I am having Bunch of Files in A folder which is shared on Network Drive . I am trying to Access those Files into my Code . But It is giving an error:
System.IO.DirectoryNotFoundException was unhandled by user code
Fname = txtwbs.Text;
DirectoryInfo objDir = new DirectoryInfo("Y:\\");
_xmlpath = objDir + "\\" + Fname + "\\" + Fname + ".xml";
if (File.Exists(_xmlpath ))
{
reader(_xmlpath);
}
I have Also used:
file = fopen("\\\\10.0.2.20\\smartjobs\\Eto\\"+Fname);
I am Able to Read File from My Local PC But it is giving Exception Only for Network Location .Please let me know how can I read File From Network Shared Location .
And Also How Can I Make A tree view of Folders into Asp.net Web Application .
Directory Structure is Like that
\\10.0.2.20\Smartjobs\Eto\
this is Parent Directory It is congaing Nos of Folder having XML Documents.
In asp.net, you cannot access network folder directly because asp.net runs under anonymous user account, that account does not have access to that location.
You can give rights to "Everyone" in that shared location and see if it is working. However this is not advisable.
Alternativly You may have to do impersonation in asp.net code when accessing network location. You will have to do implersonation with the user who has access to that shared location.
You may have map the shared directory as a user, but you forget that the asp.net is running under the account of the pool, and there you do not have connect the y:\ with the shared directory.
The next think that you can do is to direct try to connect via the network shared name, eg: \\SharedCom\fulldir\file.xml
You need to specify that the ASP.net page run as a certain user with access to the file. Then, you need to enable impersonation in your web.config file in order for ASP.net to actually access the file as that user.
Your Y drive is a mapped network drive. You need to use the network
url eg \\server\Smartjobs\Eto\xyz.xml
You specify the name of the file on the network just like you do from anywhere else:
Dim myStream As IO.FileStream = IO.File.Open("\\myserver\myshare\myfile", IO.FileMode.Open)
Dim myBytes As Byte()
myStream.Read(myBytes, 0, numberOfBytesToRead)
More reference:
Unable to List File or Directory Contents on ASP.NET Page using Shared Drive
Using file on network via IIS

FTP virtual paths. How to know real Windows path?

I have shared hosting account on some hosting service provider. So, I can upload pics or something else through FTP. For instance, my ASP .NET binaries are in /mysite.com/www/bin. But how can I set logging to file in my app? I should use real paths for that purpose. I even try just write to current directory, but I can see this log file through FTP. It just writes to some tmp path as I guess, like '' which couldn't be accessed through FTP.
So how can I setup file path for logging?
This is how I try to write to current binaries directory:
using (StreamWriter sw = new StreamWriter(String.Format("{0}{1}{2}", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "\", "log.txt"), true))
{
sw.WriteLine(DateTime.Now.ToString() + ": " + msg);
sw.WriteLine("------------");
sw.Flush();
sw.Close();
}
It write to some long windows path, which I guess it uses for some temporary storing of binaries.
Assuming your question is "I have FTP server running and serving content of "C:\myFTP\" how can I write log files (i.e. from ASP.Net application) to file available via FTP from the server?"
Answer - just write logs to that "C:\myFTP" location (or whatever location your local FTP server is configured to serve.
I solved it myself. You should use HttpContext.Current.Server.MapPath for that.

Categories