How to get the filename's path

Sep 13, 2008 at 5:22 AM
Hi,

  I am using the Custom File Uploader Adapter and has this method:

       public override void ProcessFile(HttpPostedFile file, Hashtable state)
        {

        }

When I look at the file.Filename property from the parameters, it only provides me the fully qualified filename, but not the filename's path.  Is there a way to get this from this control?  I really need the path so I can upload it to Smugmug.

Thanks!
wuttrain

Coordinator
Sep 13, 2008 at 6:46 AM
Edited Sep 13, 2008 at 6:55 AM
The path of the file is private information and is not transfered because it is not safe to do so. But I don't know why you would need that. If you recreate it on the server then wouldn't that cause potential conflicts between users?

Any way the fact is that the flash sandbox does not give us access to this information and this is not a bad thing. This is a good thing because it keeps the private information private.
Sep 13, 2008 at 1:31 PM
Hi flajaxian,

  I tried using the standard FileUpload control and when I looked at the PostedFile.Filename property of this control it contains the full path of the filename.  The PostedFile property of the FileUpload control is System.Web.HttpPostedFile just like what is used in the ProcessFile(HttpPostedFile file, HashTable state).  Without giving the full path, and I call the File.Exists function, it doesn't find it.

Thanks!
wuttrain
Coordinator
Sep 13, 2008 at 6:49 PM
Edited Sep 13, 2008 at 6:51 PM
Hi wuttrain,

Do you realize that the path given to you by HttpPostedFile is path on the client machine not on the server? That means that you can call File.Exists on your local computer because in this case you are both server and client but if you deploy that to a web site it's never going to work.

Sorry but I still don't understand why you need path? This is an information of the location of the file on the client machine. If you are trying to rebuild the folder tree for EACH client on the server then you need other type of control. Some kind of ActiveX. If you want to base your code on Flash and thus benefit from the fact that 99.9% of your users already have it then you have to comply with the constraints of the sandbox. And remember that 99.9% have Flash because it is secure enough to have those constraints. If you create your own ActiveX how many will be willing to install it? I wouldn't, if as a result you would have access to my private information.
Sep 13, 2008 at 9:01 PM
Hi flajaxian,

  You are right.  It works on my development pc but when I deployed it to my web site it never worked.

  So this means that since I want to upload the image to my Smugmug account, that would mean I will upload if first to my website's server, then from my web server to Smugmug server?

  The FolderName property of the FileSaverAdapter adapter should exist on my web server right?

Thanks a lot,
wuttrain
Coordinator
Sep 13, 2008 at 10:39 PM
Edited Sep 13, 2008 at 10:40 PM
FolderName property of the FileSaverAdapter adapter defines the name of the folder where you want your files to be uploaded.

And sorry, I am not familiar with Smugmug server. But generally you would always want to store the files on the server if they were to be viewd by other users.
Sep 14, 2008 at 1:26 AM

Hi flajaxian,

  OK, here is what I did, I'd figure I'd post the code so you could see if this is the right thing to do, or there is a better solution.  I just need to delete the file from my webserver though to make sure I don't use up my storage.  I just want to mention that your control is a very wonderful and useful to me.  More power to you!!!

  I have added this in the body of my html:
    <fjx:FileUploader ID="FileUploader1" runat="server" >
    </fjx:FileUploader>

  And in my codebehind, I added this:

Protected Sub FileUploader1_FileReceived(ByVal sender As Object, ByVal e As com.flajaxian.FileReceivedEventArgs) Handles FileUploader1.FileReceived
        Dim strDestination As String = "c:\site1\test\" & UserName & "_" & e.File.FileName

        e.File.SaveAs(strDestination)

        ' upload code here for smugmug
        '...

        ' delete file from my webserver       
        System.IO.File.Delete(strDestination)
End Sub

Coordinator
Sep 14, 2008 at 3:10 AM
I don't think you need to save the file. How do you upload to smugmug? You can use the stream directly from FileReceivedEventArgs

args.File.InputStream
Sep 14, 2008 at 4:43 AM
Hi flajaxian,

  Below is a stripped down code for smugmug upload, I hope this is still readable.  How should I make use of e.File.InputStream in this code?

      using (Stream dataStream = uploadRequest.GetRequestStream())
            {
                Byte[] arr = GetImageData(fileName);
                dataStream.Write(arr, 0, (int)arr.Length);
                dataStream.Close();
            }

private static byte[] GetImageData(string fileName)
        {

            using (FileStream f = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                FileInfo fi = new FileInfo(fileName);

                if (fi.Length > int.MaxValue) throw new InvalidOperationException("File length > int.MaxValue");

                byte[] dataArray = new byte[fi.Length];
                f.Read(dataArray, 0, (int)fi.Length);

                Trace.WriteLine("image is " + fi.Length + " bytes");
                return dataArray;
            }

        }


Coordinator
Sep 14, 2008 at 4:52 AM

  using (Stream dataStream = uploadRequest.GetRequestStream())
  {
    BinaryCopy(args.File.InputStream, dataStream, 0x200);
  }

private static void BinaryCopy(Stream from, Stream to, int bufferSize)
{
    int num;
    byte[] buffer = new byte[bufferSize];
    while ((num = from.Read(buffer, 0, buffer.Length)) > 0)
    {
        to.Write(buffer, 0, num);
    }
}

Coordinator
Sep 14, 2008 at 4:55 AM
Edited Sep 14, 2008 at 4:56 AM
P.S. if you prefer more stress on the processor less on the memory consumption decrease the bufferSize
and if you prefer less stress on the processor but can deal with increased memory consumption increase the buffer size.
In general 0x200 is ok
Sep 14, 2008 at 3:26 PM
Hi flajaxian,

  Thank you for the BinaryCopy procedure.  It works!!!  I will definitely be using your control and will make sure Flajaxian will be over on my upload page.  I will update this discussion when I get that page finalized.

  Thank you again for your help!

wuttrain