I am allowing the user to upload a file(doc, pdf, excel, txt) and then i am passing in FileStream to read and then write but after opening it i am calling a stored procedure so i can store the file name, date, upload user and where i will be a copy of it. My problem is how can deal with the string filename that has been pased in the FileStream and the stored procedure wants a string filename.
string docx = #"../../TestFiles/Test.docx";
try
{
FileStream fileStream = new FileStream(docx, FileMode.Open, FileAccess.Read);
docConverter.UpLoadFile(11, "Test.docx", "../../TestFiles/", 1, "../../Temp/", 89);
}
public void UpLoadFile(int studentId, string rawStoragePath, int uploadedByUserId, string storagePath, int assignmentElementsId)
{
Guid strGUID = Guid.NewGuid();
DateTime uploadDate = DateTime.UtcNow;
//calling stored procedure
stuSubSvc.UploadWork(studentId, strGUID, (need to pass file name), rawStoragePath, uploadDate, uploadedByUserId, storagePath, 0, assignmentElementsId);
}
Help with:
1 - getting file name from file in FileStream
2 - getting path of the uploaded file from FileStream
To get the filename out of full path you can use:
Path.GetFileName(".././Test.docx")
It'll give you Test.docx
you can't get the path of the file on the client machine. The FileStrame has a property called Handle that contains the native handle of the opened file, you follow the following article it will help you to get the file name by the handle, it is in C++ but the all functions are APIs can be called using P/Invoke in C# [DllImport].
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366789(v=vs.85).aspx
Edit
Sorry the Handle is obsolete use SafeFileHandle instead
1 - getting file name from file in FileStream
There's no filename in a stream. You can get the filename of the uploaded file from the HttpPostedFileBase instance that your controller action receives as parameter. Take a look at Phil Haack's blog post about uploading files in an ASP.NET MVC application in which he illustrates that.
Assuming you have already stored the file on your file system, you could retrieve the filename from the Name property of the FileStream:
string filename = Path.GetFileName(fileStream.Name);
2 - getting path of the uploaded file from FileStream
There's no filename nor filepath in a stream. All you can hope of getting is the filename which is contained in the HttpPostedFileBase instance that your controller action should receive as parameter (see previous point).
Related
I'm working with print jobs using PrintSystemJobInfo and this class doesn't have the path of the file (print job). So, I was wondering if there is a class where I can use the filename that is open (in memory) and this class return the full path. This file opened could be .doc, .pdf, .xls, .txt, and so on.
Please, someone can point me to the right direction or have an idea... it would be very helpful...
The only way for you to find open file handles is to use the NtQuerySystemInformation call. Here is a project that has this done as an explorer context menu. In this guy's case, he looks for files open in a specific folder.
You would then have to match the file name to the file you have in your print job.
By the way, this is not C# but you can wrap and call the same calls he is using. The rest is really up to you to figure out. ;)
Assuming you have a Stream object that is a FileStream then just do a cast and interrogation:
Stream str = printJob.JobStream;
FileStream fileStream = str as FileStream
if( fileStream != null ) {
String fileName = fileStream.Name;
}
I would like to get the full path of the file upload control in the string variable. The file may be stored in any location other than the root of the project. Anybody please help out.
The situation is:
string file = Path.GetFileName(ExcelFileUpload.FileName);
if (file.EndsWith(".xlsx"))
{
// Reading from a binary Excel file (format; *.xlsx)
FileStream stream = File.Open(file, FileMode.Open, FileAccess.Read);
It sounds like you're actually asking for the original path to the file on the client machine.
This is (a) useless (it's on a different computer) and (b) impossible to get (the browser doesn't tell you it).
What are you trying to do?
You could try somehting like this : (where MyFileUploader is your FileUpload control)
string fileBasePath = Server.MapPath("~/");
string fileName = Path.GetFileName(this.MyFileUploader.FileName);
string fullFilePath = fileBasePath + fileName;
Is there a simple way to get the FileInfo object from an HttpPostedFileBase? I realize I can save the file then do something like DirectoryInfo.GetFiles and then loop through the files looking for my file, but is there any simpler way to do this for a given file?
There's no FileInfo associated to an uploaded file. Only the filename is sent as parameter as well as the file stream itself. So that's what you could query:
HttpPostedFileBase file = ...
string filename = file.FileName;
int fileSize = file.ContentLength;
string contentType = file.ContentType;
using (Stream stream = file.InputStream)
{
// do something with the file contents here
}
To better understand what gets sent from the client I invite you to read the multipart/form-data specification.
The FileInfo object contains things like LastModified and LastAccessed date which is not an information that is sent when a file is uploaded. If you save the file on your web server disk and then retrieve the FileInfo from it bear in mind that what you will be retrieving is the information about this file on the server and not on the client simply because this information is never sent when a file is uploaded.
I am using Uploadify to upload multiple files in my ASP.NET MVC application. In the controller action, I need to check if one of the uploaded files is a zip file, and if yes, I need to check its contents. For the zip functionality I am using the ICSharpCode.SharpZipLib.
When uploading a zip file from say my desktop, I am getting the following error:
Could not find file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\xyz.zip' on the following line of code:
FileStream fs = System.IO.File.OpenRead(Path.GetFullPath(fileData.FileName));
ZipFile zf = new ZipFile(fs);
How do I get past this error?
[HttpPost]
public ActionResult Upload(HttpPostedFileBase fileData)
{
if (fileData != null && fileData.ContentLength > 0)
{
if (Path.GetExtension(fileData.FileName) == ".zip")
{
FileStream fs = System.IO.File.OpenRead(Path.GetFullPath(fileData.FileName));
ZipFile zf = new ZipFile(fs);
foreach (ZipEntry zipEntry in zf)
{
}
}
else
{
var fileName = Server.MapPath("~/Content/uploads/" + Path.GetFileName(fileData.FileName));
fileData.SaveAs(fileName);
return Json(true);
}
}
return Json(false);
}
HttpPostedFileBase.FileName is the name of the file uploaded, not the location on the file stored on the server. HttpPostedFileBase does not store the file on the server, only as a stream. Your options are either open the stream in memory (if your 3rd party utilies allow for opening streams) or saving the file to a known location, then open it from that location.
Path.GetFullPath gets a full path from the current directory.
That has nothing to do with your HttpUploadedFileBase, which isn't on disk.
You need to pass a stream instead of a file path.
If you want to check if it is a zip file, you could always look at the content-type coming back.
application/zip
This might work for what you are trying to do. Also, just try and look at what the content-type, you might find something more specific to your needs.
I want to name the file dynamically in C#.
i.e) Name of the File will be picked from Database. When i generate the Excel File and save in a working folder, the file name should be picked from the variable !!
i am searching online to find the solution !!
Presumably when you generate the file, one of the method calls (e.g. SaveAs) takes the name of the file as a parameter. So just don't hard-code that argument... use the value fetched from the database instead.
If that isn't enough information, please clarify your question.
Here you go.
string sFileName = "ExcelFile";
FileStream fExcel = new FileStream(Application.StartupPath + "\\" + sFileName, FileMode.Append, FileAccess.Write);