Manually Specify File Name

Jun 17, 2008 at 2:56 PM
Thanks for the great control. 

I'd like to rename the file before it's saved. Can you please show me a brief example of how to accomplish this? Thanks a lot! 
Coordinator
Jun 17, 2008 at 11:23 PM

<SCRIPT runat="server">
protected void FileUploader_FileReceived(Object sender, FileReceivedEventArgs e){
    string fileName = e.File.FileName;
    // here rename the file to whatever you want
    e.File.SaveAs(YOURPATH + fileName);
}
</SCRIPT>

<fjx:FileUploader OnFileReceived="FileUploader_FileReceived" runat="server"/>

Jun 18, 2008 at 12:57 PM
Edited Jun 18, 2008 at 4:30 PM
Thanks for the reply. I was able to get the following code to work; however, I noticed that it saves the file two times: once as the original file name, and again as the new file name. Is there a way we can prevent the old file name from being saved?

Protected

Sub FileUploader_FileReceived(ByVal sender As Object, ByVal e As com.flajaxian.FileReceivedEventArgs)
      Dim strUniqueString As String = System.Guid.NewGuid().ToString()
      Dim strFileExt As String = System.IO.Path.GetExtension(e.File.FileName)

      Dim strFileName As String = strUniqueString & strFileExt
      e.File.SaveAs(Server.MapPath(
"~/Uploads/") & strFileName)
End Sub

 

 

 


Coordinator
Jun 18, 2008 at 4:30 PM
Well this is because you use both custom code and adapter, that's why I showed in the example not to use adapter if you gonna use custom code:

<fjx:FileUploader OnFileReceived="FileUploader_FileReceived" runat="server"/>

Jun 18, 2008 at 5:41 PM
Oops! Sorry, forgot to remove that bit of code. It works fine now.

One last question: Can you show me how I could get a manually specified file name into the ThumbGeneratorAdapter? I'd like to save the thumbnail with a manually specified name too.

Thanks a lot for your help!
Coordinator
Jun 18, 2008 at 9:38 PM

Well it is pretty much the same thing there. You can use this method:

private bool GenerateThumbnail(Stream stream, string thumbnailFileName, string thumbnailPath, int maxWidth, int maxHeight)
        {
            thumbnailPath = Path.GetFullPath(thumbnailPath);

            Image image = Image.FromStream(stream);

            double ratio = image.Width / (double)image.Height;
            int width = maxWidth;
            int height = (int)Math.Round(width / ratio);
            if (height > maxHeight)
            {
                height = maxHeight;
                width = (int)Math.Round(image.Height * ratio);
            }

            try
            {
                Image thumbnailImage =
                    image.GetThumbnailImage(width, height, new Image.GetThumbnailImageAbort(delegate { return true; }), IntPtr.Zero);

                thumbnailImage.Save(String.Concat(
                                        thumbnailPath,
                                        Path.DirectorySeparatorChar, 
                                        thumbnailFileName
                                        ));
                return true;
            }
            catch (Exception)
            {
                // handle error here
                return false;
            }       
}

Jun 20, 2008 at 4:58 PM
Edited Jun 20, 2008 at 5:37 PM

Can you confirm that the following code is the correct way to pass the stream to the function you provided? (I converted everything to VB). It's not working and I suspect it may be an unrelated issue:

      Protected Sub FileUploader_FileReceived(ByVal sender As Object, ByVal e As com.flajaxian.FileReceivedEventArgs)
            ...
            GenerateThumbnail(e.File.InputStream, strFileNameThumb,

"~/Pictures", 200, 200)
      End Sub

I'm catching this error in a try:
      System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
      at ASP.PicturesRT.GenerateThumbnail(Stream stream, String thumbnailFileName, String thumbnailPath, Double maxWidth, Double maxHeight)
      at ASP.PicturesRT.FileUploader_FileReceived(Object sender, FileReceivedEventArgs e) in E:\Inetpub\TestingEnvironment\Safety\Controls\PicturesRT.ascx:line 53

Thanks!

 

 

 

Coordinator
Jun 20, 2008 at 10:00 PM
So please get the latest source or binary where you could use FileNameDetermening event in FileSaverAdapter or ThumbGeneratorAdapter.

Here is an example that saves files with prefix _File_ and thumbs with prefix _Thumb_

<%@ Import Namespace="System.IO" %>
<SCRIPT language="C#" runat="server">
protected void FileNameDetermening(object sender, FileNameDetermeningEventArgs args)
{
    args.FileName =
        Path.GetDirectoryName(args.FileName) +
        Path.DirectorySeparatorChar + "_File_" +
        Path.GetFileNameWithoutExtension(args.FileName) +
        Path.GetExtension(args.FileName);
}
protected void ThumbNameDetermening(object sender, FileNameDetermeningEventArgs args)
{
    args.FileName =
           Path.GetDirectoryName(args.FileName) +
           Path.DirectorySeparatorChar + "_Thumb_" +
           Path.GetFileNameWithoutExtension(args.FileName) +
           Path.GetExtension(args.FileName);
}
</SCRIPT>

<fjx:FileUploader ID="FileUploader1" runat="server" >
    <Adapters>
        <fjx:FileSaverAdapter Runat="server" OnFileNameDetermening="FileNameDetermening" FolderName="UploadFolder" />
        <fjx:ThumbGeneratorAdapter Runat="server" OnFileNameDetermening="ThumbNameDetermening" FolderName="UploadFolder" Suffix="_thumb" MaximumWidth="200" MaximumHeight="200" />
    </Adapters>
</fjx:FileUploader>