By Daniel Du
We had some issue with our viewer live sample - SAP integration demo - last week. After investigation, it turns out that it is just due to a small annoy thing – the encoding of filename. I am sharing my experience so that you don’t walk into the same issue latter.
To view the model, we need to upload the model file to Autodesk cloud, and register it for translation. The file identifier in cloud will be an URL like baseurl/bucket/{bucket_key}/objects/{object_key}. Generally speaking, the object key is the file name of model. As you know, we need to encode the filename to serve as object key, right?
I firstly use following JavaScript code to do encoding:
var objectKey = escape(filename);
In most time, it works just fine. But recently, we run into trouble, because one filename has a special character plus (‘+’). escape() does not encode the ‘+’ character. Following are some testing:
var filename = 'file name + somethig.txt';
console.log(escape(filename));
> file%20name%20+%20somethig.txt
console.log(encodeURI(filename));
> file%20name%20+%20somethig.txt
console.log(encodeURIComponent(filename));
> file%20name%20%2B%20somethig.txt
Form the result, you notice that only encodeURIComponent encodes the plus sign(‘+’) to “%2B”. So we need to use encodeURIComponent to encode filenames when uploading with View and Data API, otherwise you will run into problem. Not a big deal but it takes time to figure out why :s
If you encode from server side with C#, the commonly used HttpUtility.UrlEncode() is also problematic for this scenario. We should use Uri.EscapeDataString() instead.
Hope this helps some if you run into the same issue.
Comments
You can follow this conversation by subscribing to the comment feed for this post.