asp.net File.Copy Lock - asp.net

I have an Asp.net page which allows file uploads and then copies the newly uploaded file to another folder. So far so good. Problem is that the copied file is used in the other folder for some other needs (e.g. using FileStream to open the copied file). But asp.net puts a lock on the copied file. I can't even open the copied file using Windows Explorer so long as the Visual Studio's builtin web server is running. I get an error like:
The process cannot access the file 'C:\inetpub\wwwroot.....pdf' because it is being used by another process
Here is partial code:
System.IO.File.Copy(targetFolder_live + "\\" + finalfilename, targetFolder_encr + "\\" + finalfilename);
createpreviewdoc(finalfilename);
////inside the createpreviewdoc function
FileStream stream = new FileStream(targetFolder_encr + "/" + file, FileMode.OpenOrCreate);//Fails with the error!
So what can be done to remove the lock in the same page load? Thanks!

I would attempt the copy in a different fashion.
FileInfo fileInfo = new FileInfo("PathToOriginal");
fileInfo.CopyTo("MyNewLocation");
File.OpenText("MyNewLocation"); // works for me

Related

Download file and open it without saving it in downloads folder

We try to download a file from the web server (.NET 5 / MVC) and open it automatically on the client side with the specific appliation without saving the file on the local disk. In our case we download a CAD file (*.dwg) and open it with DWG TrueView.
The file gets downloaded and application will be started as well. But every time we click on a file in out web application, the browser save a copy of the file in the downloads folder.
What is the correct approach to solve this problem?
Citrix does the same with ICA files - once I tell the browser it should open ICA files, it will not save the files on the disk.
This is roughly how the code looks like:
public FileStreamResult DownloadFile()
{
var fs = new FileStream("myFilePath....", FileMode.Open, FileAccess.Read);
return new File(fs,"application/octet-stream","myFile.dwg");
}
We also tried all sorts of content-types without success.

ASP.net file upload from a non server - issue with Server.MapPath?

I am trying to upload a file in asp.net using the following code
Dim FileName As String = System.IO.Path.GetFileName(ClientFileName)
MyFile.PostedFile.SaveAs(Server.MapPath("~/UploadedImportedFiles/" + FileName))
if the file being uploaded (say book1.xls) resides on the machine that is also the server all works perfectly, but if the file resides on a Pc that is not the server it fails on the second line. I think the problem is that Server.MapPath seems to refer to the non server PC when it is uploaded from there.
Thanks
You wrongly getting file name. You should use below code
string filename = Path.GetFileName(FileUploadControl.FileName);
Of course change control name to your own.
Please See: http://msdn.microsoft.com/en-us/library/aa479405.aspx

FileUpload control (UploadButton.PostedFile.FileName)

Recently i developed a code on asp that requires to upload a file up to a server.
As i found out from the web, in order to view the local file of a file it can be done by doing UploadButton.PostedFile.Filename.
string fileName = UploadButton.PostedFile.Filename;
This will show the whole local path(eg. C:\Documents and Settings\christopher.lim\Desktop\HelloWorld.txt).
This work fine if it is run the code on my desktop (where my PC is the server itself) but when i shifted the code over to a test server and tried it on my desktop(PC is the client), it only display my file name instead of the whole path.
Example:
string fileName = UploadButton.PostedFile.Filename;
Response.Write("FileName: " + fileName);
1) Local PC -> C:\Documents and Settings\christopher.lim\Desktop\HelloWorld.txt
2) Test Server -> HelloWorld.txt
P/S: Sorry if it's confusing because i am new to server client. Correct me if i'm wrong.
As per MSDN-
The file name that the FileName property returns does not include the
path of the file on the client.
While it is true that on a local system you can get the complete path, but while you run it on a server it will only return the name of the file.
Also FYI the file upload control behaves differently in different browsers. In firefox you ill probably get only the file name and not the full path using fileupload.postedfile.filename and in IE the same thing may show you the full path.
However the path of the file uploaded from client system shouldn't matter as only the file name is more than enough, but if you still have the need try- Path.GetFileName(filename) MSDN link

asp.net File.OpenText can't read uploaded file

I have an ASP.NET MVC application on Windows Server 2008.
I need to upload a file, save it to an archive folder under the App_Data folder, then open and read from it. I can do this on my local machine but can't on the test server. I suspect it is a permissions issue but the permissions appear to be in place. The relevant C# code:
HttpPostedFileBase hpf = Request.Files[0];
var fileLength = hpf.ContentLength;
if (fileLength != 0)
{
var archiveFolder = Server.MapPath("~/" + folder);
var archiveFile = "import_" + DateTime.Now.ToString("yyyyMMdd_HHmm") + ".txt";
var archivePath = Path.Combine(archiveFolder, archiveFile);
hpf.SaveAs(archivePath);
}
The above code saves the file fine.
StreamReader sr = File.OpenText(archivePath);
The above line throws an error:
Could not find file 'c:\windows\system32\inetsrv\usb01312012.txt'.
So although "archivePath" is a path to a saved file under App_Data, ASP.NET looks to the SYSTEM folder for the file.
I have given every permission except Full Control to IIS_USRS on the entire website. Why can't I access the file?
Make sure your site runs correct version of asp.net (2.0 or 4.0).
RESOLVED: As of the next day, this now works! I guess IIS had to recycle? Thanks for your comments!

ASP .NET invalid path

I have a web page which prompts the user for an excel file using the fileupload control. What it then does is read the file into a datatable using an OleDbConnection and then runs other code with that data. When I test in Visual Studio, it works fine. For example, I can look up a file 'g:\myfiles\upldtest.xls', it finds the file, reads it and the code works. When I try to run it on our web server, I get an error when it tries to create an OleDBConnection saying It is trying to create an OleDbConnection and the path 'g:\myfiles\upldtest.xls' is invalid.
I tried to use ManagementObjectSearcher to convert the connection string path to UNC (\\MyDataServer\myfiles instead of g:\myfiles). When I test it, it shows the correct path but when I upload the page to the web server, I still get the path 'g:\myfiles\upldtest.xls' is invalid.
The code I use to determine the required file name is this
string tname = FileUpload1.PostedFile.FileName; //the file name and path
string gname = tname.Substring(tname.LastIndexOf("\\") + 1); //The path name
Any ideas what I am missing? My contract requires me to use VS2005 and .NET framework 2.0 so, I can't use anything newer. Thanks in advance for the assistance.
HttpPostedFile.FileName returns the fully qualified name of the file on the client machine.
You need to call SaveAs() to actually save the file on the server:
using System.IO;
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string filepath = Path.Combine(#"X:\Your\Own\Upload\Folder", filename);
FileUpload1.PostedFile.SaveAs(filepath);
// Now use `filepath` as your data source.
IIS might have already written the file in a temporary location to save memory, but since you can't (and shouldn't) access that location, it makes no difference.
You should also be aware of cross-browser issues. IE sends the whole path to the server on file upload, while Firefox/Chrome do not.

Resources