How to get file path using FileUpload to be read by FileStream? - asp.net

I have a Method that open excel file and read it through exceldatareaderClass that i have downloaded in codeplex by using filestream.
Currently I just declared the exact directory where the filestream open an excel file.And it works fine.
Stream stream = new FileStream("C:\\" + FileUpload.PostedFile.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
Now i need to read the excel file wherever location the user place it like on windows forms fileupload.FileStream needs the exact location where the file is located. How to do this.?
Example: Sample.xls is located on My Documents
the file path should be like : C:\Documents and Settings\user\My Documents\
string openpath ="" ;//filepath
Stream stream = new FileStream(openpath+ FileUpload.PostedFile.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
Thanks in Regards

The FileUpload property PostedFile.InputStream already represents the stream that you create in your example.
Note that the FileName property stores the file name on the client, not the file name on the server.
You cannot expect that the uploaded file is present as a physical file on the server, just use the InputStream to read the uploaded data.

Related

Why the file stream is reading wrong path?

I am trying to access textfile present in the folder inside the solution.
Dim fs As New FileStream("../CMMS/Webservices_URL.txt", FileMode.Open, FileAccess.Read)
but it always picks c drive even though the solution is somewhere else. why it's happening?
Folder1/Folder2/CMMS/Webservices_URL.txt is the actual path but it picks c:/CMMS.
To get the physical path of a file in Web Forms you need to call Server.MapPath("~/Folder1/Folder2/CMMS/Webservices_URL.txt")
you can find the details here:
https://learn.microsoft.com/en-us/dotnet/api/system.web.httpserverutility.mappath?view=netframework-4.8

Not found as file or resource

I am adding text to existing pdf using Itextsharp in xamarin forms UWP. For this I am reading the source file using PdfReader like
string source = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Test.pdf");
string target = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Test_1.pdf");
PdfReader reader = new PdfReader(source);
when ever PdfReader read's the file it is showing exception like
C:\Users\ ...\Documents\Test.pdf not found as file or resource.
Please help me for how to read the file using PdfReader.

Save StringBuilder to disk in .CSV format

I have a StringBuilder object which I have built with comma seperated values, which I would like to save as a .csv file.
I know how to stream the data as CSV, but how can I physically save the data as a .csv file, on the server?
Currently I stream the CSV like the following, where sb is StringBuilder:
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment;filename=List.csv")
Response.ContentType = "text/csv"
Response.Write(sb.ToString)
Response.End()
I assume you already know how to create csv, so check this question that shows how to save stream to file. As you're using ASP.NET just make sure you have the appropriate permissions to modify.
Conclusion
As you want to make the download available to user(instead of save it in the server) and csv is basically a text file check out this
It's not different than writing a normal file. If you want to save the file to a folder in your application, you should use Server.MapPath("~\MyCsvFiles") to get the physical path of the file.
Depending on the version of IIS, ASPNET or NETWORK SERVICE needs write permissions on the server folder you are writing to; then just write the CSV file out with StreamWriter or your preferred method. If you really want an Excel file, then consider using EPPlus.

DotNetZip library "Access to the path denied"

I am trying to create a zip file and save it using DotNetZip library in ASP.NET application.
But for some reason i get a
Access to the path is denied
error when i try to save it.
I changed the TempFileFolder to another folder and have given permissions to it. Still no luck.
Dim zipFile As New ZipFile()
zipFile.AddFile(filePath)
Dim tempFilePath As String = "Report.zip"
zipFile.TempFileFolder = "D:\Temp\"
zipFile.Save(tempFilePath);
I found a question but the answer did not help me.
From the above question, one answer mentioned:
Also, the tempFilePath in your example doesn't include a full path, could it be that it is trying to save the ZIP into a different folder from the one you are expecting (and have assigned permissions to)?
How to figure out to which folder it is trying to save even though I mentioned TempFileFolder as D:\temp\?
Any thoughts?
Since you said you 'gave permissions' I'm assuming that you provided the account(s) which run the ASP.NET and IIS processes file Read/Write permissions to the folder where you're trying to save this file.
The 'Temp File Folder' is just what its name describes: a temporary file folder. It's a holding place in case the library needs to do some file I/O. it's not a base file.
Modify the code to provide a fully qualified path name to save the file to:
Dim zipFile As New ZipFile()
zipFile.AddFile(filePath)
Dim tempFilePath As String = "D:\Temp\Report.zip"
zipFile.TempFileFolder = "D:\Temp\"
zipFile.Save(tempFilePath)
Check if the file is not Read Only

How to create a file on server and save it on server

I m creating a pdf file on the server using iTextSharp.
It is working fine prompting me to save everything is fine.
I have 2 scenario
1) I want to save this file on a location on server
2) I want to save this file in database
My Last line after finishing the code is
document.Close();
So can anyone guide me how can I do it
The following method will create a new file on the disk with a paragraph in it. You could easily modify it to your needs.
Before this will work you must ensue the directory you wish to save to has the correct permissions set. Whatever user account the Application Pool for the website is using will need write access to the folder. Set this in Windows Explorer, Right Click > Properties > Security Tab. This is well documented on the internet.
You can store the filename in the database in many different ways and then link to the file from your UI. I suggest starting with a simple ADO.Net tutorial
protected void CreateDoc(string filename)
{
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create));
document.Open();
document.Add(new Paragraph("Hello World!"));
document.Close();
}

Resources