Uploading files in C# from any location

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mbewers1
    New Member
    • Feb 2009
    • 68

    Uploading files in C# from any location

    I'm attempting to create a method in my code which allows users to upload files from any location on their desktop.

    So far, every every attempt I've used to try and code this, I get an error saying that 'Access to the path has been denied'. The error doesn't appear when I place files in the same directory in which Visual Studio is located however, uploading from there is not convenient for users.

    I have looked at the FileInfo class (.DirectoryName , .FullName) and the properties within the FileUploader class (.FileName) but, unfortunately, none of these allow me to perform the action.

    Can anyone suggest what library operations I need to upload files from anywhere and not just from the VisualStudio program?
  • semomaniz
    Recognized Expert New Member
    • Oct 2007
    • 210

    #2
    where is your file located? Is it in a server or a local computer?

    Comment

    • mbewers1
      New Member
      • Feb 2009
      • 68

      #3
      It's on my local PC. So is my code but, as mentioned, I am only able to upload from 1 location at the moment and that is the VisualStudio directory.

      Comment

      • semomaniz
        Recognized Expert New Member
        • Oct 2007
        • 210

        #4
        Post you code please

        Comment

        • mbewers1
          New Member
          • Feb 2009
          • 68

          #5
          Posted code for: Uploading files in C# from any location

          OK well, it's all in bits between different classes.
          The following method takes a string argument which is taken from a FileUploader tool:

          FileUploader.Po stedFile.FileNa me

          Code:
          private void CheckFileSelection(string fileName)
              {
                  if(techFileUpload.HasFile)
                  {
                      if (reflista.SelectedValue == "BibTeX" || fileName.ToLower().EndsWith(".bib"))
                      {
                          BibTexItemTypes bit = new BibTexItemTypes(fileName);
                          itemCount = bit.ReadBibTeXFile(fileName);
                          if (itemCount == 0)
                          {
                              message.Text = @"No BibTeX records were found within the file
                                 and so, no records have been imported";
                          }
                          else
                          {
                              message.Text = string.Format(@"BibTeX record imported successfully, 
                              {0} items found, {0} items added", itemCount);
                          }
                      }
          The next part reads the contents of the file using a StreamReader and uses a
          File.OpenText property to start this off, don't know if this makes any difference but I've posted it anyway.

          Code:
          public int ReadBibTeXFile(string fileName)
              {
                  using (StreamReader sr = File.OpenText(fileName))
                  {
                      while (!sr.EndOfStream)
                      {
                          string line = sr.ReadLine();
          
                          if (line.Contains("@article"))
                          {
                              ArticleProcess(sr);
                          }
           
                          // other statements
          Someone else told me a while back that there is some sort of a 'FileManager' tool to help this but it doesn't appear that this is a class or a tool that can be used within the .NET framework.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            where is your file located? Is it in a server or a local computer?
            It's on my local PC.
            I suspect you are getting lost in termonolgy.

            If the source file is on your PC, and you are just copying it to the desktop then it isn't an upload - its a file copy and can be done with

            System.IO.File. Copy(string source, string destination);

            If the source file is on an internet server someplace then you still aren't uploading - you are downloading. The files come DOWN from a server, or UP to a server. Which means if you have the terms backwords you are probably using the wrong tool for you intention.

            Comment

            • mbewers1
              New Member
              • Feb 2009
              • 68

              #7
              Avoiding errors in uploading

              I didn't want to move a file to another location, I wanted to store the details in the database but It's OK, I've solved the problem.

              What I did was add the line:

              Code:
              byte[] byteArr = File.ReadAllBytes(fileName)
              ;

              to read the file as a byte array, then I passed the byte array to my file-handling classed called BibTexItemTypes .

              Code:
              BibTexItemTypes bit = new BibTexItemTypes(byteArr);
              Then plugged the byte array into a StreamReader, so we could read the file line by line and determine therefore, which parts of the file go into the database and where.

              Code:
              Stream s = new MemoryStream(byteArr);
              StreamReader bibTex = new StreamReader(s);

              Comment

              Working...