MennaKearns
Crownpeak (Retired)

How to change an attached file's name

When using a Document Selector control (ie. Input.ShowAcquireDocument() or Input.ShowAcquireImage()), there are two ways you can modify the the file name of the selected asset:

Upload the file as a separate asset and then select it using the document selector

Using this method, since the upload is a separate asset, it has its own unique DXM generated file path and URL. These are generally constructed by the asset label and the folder that the asset is contained in.

  1. Upload the file into DXM as a binary asset.
  2. Edit the parent asset.
  3. Using the document selector, navigate to the location where you uploaded the file in step a and then select the file.
  4. Save your asset.

Publish the uploaded file followed by the parent asset (in that order).

Select a file from your local computer directly

By default, files selected from your local machine will be published with a default CMS generated filename. For an example of the filename output, please see here: Uploaded Asset Filename

  1. Utilize the asseturl.aspx and assetfilename.aspx in the template to change the default filename.
  2. Create/Add the asseturl.aspx and assetfilename.aspx template files to the template.
  3. In the assetfilename.aspx, you will need to set the context.PublishPath value to the desired publish path (ex. /folder/filename.aspx).
  4. In the asseturl.aspx, you will need to set the context.PublishUrl value to be the desired url (ex. /folder/filename.aspx).
  5. Publish the asset.

    Please note that the asseturl.aspx and assetfilename.aspx will be run for each file uploaded into the asset.

    The code below provides an example of how to change the default CMS generated filename to the original filename for all uploaded files.

    Code for asseturl.aspx, used to modify the url of the uploaded asset.
string strPath = context.PublishPath;
    string publishedFilename = context.RemotePath.FileName; // Get the filename path 
    IEnumerable<KeyValuePair<string, UploadedFile>> uploadedFiles = asset.UploadedFiles.Cast<KeyValuePair<string, UploadedFile>>(); // Cast the Uploaded File List as a KeyValuePair
    KeyValuePair<string, UploadedFile> kvp = uploadedFiles.Where(kv => kv.Value.Path.Contains(publishedFilename)).FirstOrDefault();//Find the first Uploaded File with the desired Filename
    

    if(!kvp.Equals(default(KeyValuePair<string, UploadedFile>)))//Make sure the filename isn't default
    {
        strPath = strPath.Replace(publishedFilename, asset["upload_name#" + kvp.Key]);//Replace the filename with the desired filename
    }
    context.PublishUrl = strPath;//Set the publishURL

Code for assetfilename.aspx, used the change the server side path of the uploaded asset

string strPath = context.PublishPath;
    string publishedFilename = context.RemotePath.FileName; // Get the filename path 
    IEnumerable<KeyValuePair<string, UploadedFile>> uploadedFiles = asset.UploadedFiles.Cast<KeyValuePair<string, UploadedFile>>(); // Cast the Uploaded File List as a KeyValuePair
    KeyValuePair<string, UploadedFile> kvp = uploadedFiles.Where(kv => kv.Value.Path.Contains(publishedFilename)).FirstOrDefault();//Find the first Uploaded File with the desired Filename
    

    if(!kvp.Equals(default(KeyValuePair<string, UploadedFile>)))//Make sure the filename isn't default
    {
        strPath = strPath.Replace(publishedFilename, asset["upload_name#" + kvp.Key]);//Replace the filename with the desired filename
    }
    context.PublishPath = strPath;//Set the PublishPath

When a file is uploaded from your local machine, the document selector control will save the original filename into a content field prefixed with "upload_name#." The code above replaces the default CMS generated filename with the value saved in this field.

Please be careful as when modifying the assetfilename.aspx or asseturl.aspx as it could potentially cause conflicts with other assets if both assets are publishing to the same location.

Labels (1)

Can't find what you are looking for?

Find Answers

Search our DXM Forum to find answers to questions asked by other DXM users.

Ask a Question

No luck? Ask a question. Our Product and Support teams are monitoring the Forum and typically respond within 48 hours.

Ask a Question