Reputation: 2505
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
Upvotes: 0
Views: 2166
Reputation: 1794
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
Upvotes: 1
Reputation: 14677
To get the filename out of full path you can use:
Path.GetFileName(".././Test.docx")
It'll give you Test.docx
Upvotes: 1
Reputation: 1038890
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).
Upvotes: 0