Tuesday, June 9, 2009

ASP.NET FileUpload and File.Open() method problems

In this post I tried to explain the problems using File.Open() method when we use FileUpload control on ASPX form.

Today at my work, I faced plenty of problems with ASP FileUpload control in SharePoint. I was creating a custom form according to a client requirement. And whenever I tried to upload a file to the document library it was failing always.
The exception I am getting was: Could not find a part of the path "file upload path."

I was frustrated trying different things for 20-30 minutes and finally thought of checking the code again for a clue. Below are the details from my analysis.
I used the following code to get the uploaded file as a stream and save it to a SharePoint document library.

using (FileStream fs = File.Open(fileName, FileMode.Open))
{
    //Sharepoint programming - Adding document to a document library.
SPFile destfile = folder.Files.Add(fileName.Substring(fileName.LastIndexOf("\\") + 1), fs, true);
    //your logic here
}

fileName is a variable passed to File.Open() method that holds the file system path of the file from the file upload control.
The above code was trying to find the file in the given path on the server. If you correctly read the above sentence, it was looking for the file on the server with the given path.

Take this scenario. I was working on machine1 which is a development environment (my MOSS server) and in which my application is also running. I was testing the application from the other machine named machine2 (client). When I try to upload a document from machine2 from the path say c:\documents\abc.docx, The server code [C#] File.Open() was looking for that same path c:\documents\abc.docx on the server, i.e. on machine1. See, there was the problem. The code was trying to find the file at the path c:\documents\abc.docx on the machine1, which doesn’t exist. This is the problem with the File.Open() method in file upload control. So never use this in the code implementation in c# when you are dealing with the FileUpload.

Here is a Solution:
using(Stream fs = fileUpload.PostedFile.InputStream)
{
    //Sharepoint programming - Adding document to a document library.
SPFile destfile = folder.Files.Add(fileName.Substring(fileName.LastIndexOf("\\") + 1), fs, true);
    //your logic here
}

If you observe, I am, here taking the stream from the fileupload control by using fileUpload.PostedFile.InputStream instead of passing the file name to File.Open().

Be careful while using the streams and know the difference between the File.Open() FileStream and the FileUpload Stream.
Hope this helps you to understand the both objects how to use and where to use.

5 comments:

  1. Thanks for the info...That was really helpful as I was also going crazy with this problem...Now its working...cheers!!

    ReplyDelete
  2. Thanks for the info...That was really helpful as I was also going crazy with this problem...Now its working...cheers!!

    ReplyDelete
  3. Thank You =) That post saved me a lot of time!

    ReplyDelete
  4. Thanks a bundle .... great post ... and really helpfull i was struggling with it... but you saved my time. Thanks

    ReplyDelete
  5. this is a great post thank you so much

    ReplyDelete