SlickUpload
Welcome Guest Search | Active Topics | Log In | Register

modify filename from client filename Options
KathyW
#1 Posted : Saturday, January 07, 2012 5:12:55 PM
Rank: Newbie

Groups: Registered

Joined: 1/7/2012
Posts: 3

(Trying with Firefox.  IE9 leaves no text in the message.)

 

I'd like to modify the client filename (remove spaces, truncate if needed), and use the modified name for the server filename of the upload.  Can this be done?

Axosoft
#2 Posted : Monday, January 09, 2012 8:07:50 AM
Rank: Administration


Groups: Administrators

Joined: 7/7/2005
Posts: 1,586
Location: Scottsdale, AZ

Sure. The way to do this is through a custom UploadStreamProvider class. The easiest way is to create a class that inherits from FileUploadStreamProvider and override its GetServerName method. That method is called by SlickUpload for each file to determine which file to write to.

Take a look at the CustomFileName example -- this demonstrates how to create and configure a custom UploadStreamProvider to do filename customization.

KathyW
#3 Posted : Monday, January 09, 2012 10:01:10 AM
Rank: Newbie

Groups: Registered

Joined: 1/7/2012
Posts: 3

I'd already looked at that example (which changes the publishing location, not the filename), but I have some very basic questions.

Now, I use C# and almost no  javascript, which may be apparent from my questions (my ignorance will show).

The example is using javascript to set "fileRoot" based on a dropdown.  I'm thinking there is no equivalent need in my case?  I have a specific routine I need to do to modify the file name, not user-selected, and it would be done in the CustomFileNameUploadStreamProvider.cs file?

What is GetServerFileName supposed to return?  In the example it seems to be returning a path, but starting from where?  (Is it starting from the location folder set in the web.config, which I won't be changing?  If so, would I be returning only the filename itself?)

It's calling GetValidFileName on file.ClientName.  Is GetValidFileName defined somewhere?  What does it do?

Do I call GetValidFileName on file.ClientName, modify it as needed, and return that modified name?

 

 

Axosoft
#4 Posted : Monday, January 09, 2012 10:37:47 AM
Rank: Administration


Groups: Administrators

Joined: 7/7/2005
Posts: 1,586
Location: Scottsdale, AZ

Good questions. Yes, the sample isn't exactly what you are trying to do -- I pointed you to it to get some working code and configuration that you can modify.

You can ignore the fileRoot part and the foldering that the sample is doing if you want to simply store based on filename. The GetServerFileName method can return either a path or a filename or both.

If you return a filename or path, it will be related to the location you specify in the UploadStreamProvider configuration in web.config. For example, if you have a location="~/Files" in your web.config, and your application root is "c:\sites\yourapp", you'll get a base path of "c:\sites\yourapp\files". If you return "test.txt" from GetServerFileName, the file will be stored to "c:\sites\yourapp\files\test.txt".

You can also return a filename including a path, which will also be combined. With the data above, if you return "folder\test.txt" from GetServerFileName, the file will be stored to "c:\sites\yourapp\files\folder\test.txt".

You can also return an app relative path or absolute rooted path, in which case the base path from the location attribute is ignored. For example, if you return "~/files/test.txt" or "c:\sites\yourapp\test.txt", the file will be stored directly there, and the location attribute is ignored.

GetValidFileName is a function on the FileUploadStreamProvider class that removes any invalid characters (based on Path.GetInvalidFileNameChars) from the filename, to ensure that files uploaded don't cause errors simply because of their filename. If you're not doing any filtering of the characters in the filename other than the aforementioned space, I'd recommend passing it through GetValidFileName before you return it to ensure that you don't have issues with files uploaded from Mac's.

KathyW
#5 Posted : Monday, January 09, 2012 10:48:46 AM
Rank: Newbie

Groups: Registered

Joined: 1/7/2012
Posts: 3

Thank-you.   I now know enough to be dangerous (that is, to give it a try).  :)

Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.