- Home /
Is it possible to pull the file name from a WWW get?
I am using a WWW request to pull a file from a remote server and subsequently save it to the user's device. The problem I have encountered is that I haven't been able to extract the name of the file that is downloaded, which is a function I was relying on to allow downloading differently named files.
I don't get it... if you download a file from a server... you should already know the name of the file, no? How else can you get it?
I'm working on the assumption that it is possible clients won't give me access at the level I would prefer. I am building the download client to take any sort of url, including those generated by sites like soundcloud, which format the end or the url as download?client_id=b45b1aa10f1ac2941910a7f0d10f8e28
Answer by Bunny83 · Aug 15, 2013 at 09:10 PM
Actually there is no "filename". You send a get request to a server for a specific resource (your URL) and the server returns that resource. It doesn't have to be a file. It could be some data from a database, so there's no filename. HTTP doesn't really support a way to give the resource a "name".
Most browsers however support reading an additional Content-Disposition header which is actually not part of HTTP. If the server adds such an header you might read the possible filename from there, but most servers don't include that header.
An HTTP response just consists of:
the status line
additional headers
(an empty line)
the data
There are only a few response headers and "Content-Disposition" is the only one that even mentions a filename. So in most cases you would either:
extract the filename from the Content-Disposition header if available
extract the filename fromt the request URL which is a bit tricky since you will have a hard time to decide what is a filename and what not.
generate your own random filename (like Unity does when dragging images from the browser into Unity).
Answer by jacobschellenberg · Aug 15, 2013 at 07:46 PM
It's actually just one more step after pulling the object. In this example, I'm asking for the name
of the www.texture
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
public string url = "http://images.earthcam.comec_metrosourcams/fridays.jpg";
IEnumerator Start() {
WWW www = new WWW(url);
yield return www;
renderer.material.mainTexture = www.texture;
Debug.Log("Texture Name: " + www.texture.name);
}
}
I am downloading audio files, which are passed through .GetAudioClip(true, false, AudioType.$$anonymous$$PEG); which doesn't let me access the filename after the conversion. The debug log just returns an empty string. Unfortunately there doesn't seem to be a way to access a name field before the conversion.
Edit: The above code actually returns an empty string for the texture name.