I want to download these 2 app.zip files from a server:
https://tempapps.myserver.com/apps%2FNews%2Fapp.zip
https://tempapps.myserver.com/apps%2Fsports%2Fapp.zip
When I download these files with mozilla, it downloads them as apps_news_app.zip and apps_sports_app.zip.
I want to achieve the same in my program.
To get normal-human readable url from percent-encoded one:
QString humanReadable = QUrl::fromPercentEncoded("https://example.com/something%20here.zip");
To get percent-encoded url:
QUrl myUrl("https://example.com/something here.zip");
QString percentEncoded = myUrl.encoded();
For more information, be sure to visit the QUrl documentation.
If using java, you would use something like
String newUrl = "whatever the url is".replace("\%2F", "_");
Open http://doc.qt.io/qt-5/qstring.html and scroll to replace for info.
BTW, I am not sure about the backslash. If the above doesn't work, try different combinations of backslash or not before those argument strings.
Related
Firstly, I understand that "\" is an R escape character and also the file path separator on windows.
I know that it can be escaped by using either / or \.
I am developing a package and I want a function for the user to literally just be able to call it like:
makeFileLocationRCompatable("H:\Temp")
and for the function to return
"H:/Temp"
or
"H:\\Temp"
but it seems to be impossible in R due to the fact that \ escapes the following character.
I don't want my users to have to change the way they input the file path.
Any ideas?
You can use Rstudio snippetsaddin to convert the slash
download it from here.
devtools::install_github("sfr/RStudio-Addin-Snippets", type = "source")
Restart Rstudio.
Select the path or the code where slashes needs to be replaced.
Click on Addin -> select convert slash
It will reverse all slashes if the path is selected.
normalizePath from the base package might provide this functionality? (I cannot test on Windows myself; sorry if this is a moot proposal)
For example
normalizePath('H:\\Temp', winslash = '\\')
See also ?normalizePath
There's some path as QString:
QString path = "C:/bla/blah/x/y/file.xls";
I thought that maybe getting last offset of / would be a good start. I could then use right method (no pun intended) to get everything after that character:
path = path.right(path.lastIndexOf("/"));
or in a more compatible way:
path = path.right(std::max(path.lastIndexOf("\\"), path.lastIndexOf("/")));
Those both have the same bad result:
ah/x/y/file.xls
What's wrong here? Obviously the path is getting cut too soon, but it's even weirder it's not cut at any of / at all.
The QString method you want is mid, not right (right counts from the end of the string):
path = path.mid(path.lastIndexOf("/"));
mid has a second parameter, but when it's omitted you get the rightmost part of the string.
And for a cleaner / more universal code:
QFileInfo fi("C:/bla/blah/x/y/file.xls");
QString fileName = fi.fileName();
NB QFileInfo doesn't query the file system when it doesn't have to, and here it doesn't have to because all the infos are in the string.
From QString::right():
"Returns a substring that contains the n rightmost characters of the string."
You're using the index as a count. You'd have to use .size() - .indexOf().
I have a problem with special characters when trying to post file as "multipart/form-data". When i try to post a file with special characters, the file is missing from the post. With no special characters my script works just fine.
How should I handle the filenames?
Works ok:
r = requests.post('http://localhost/upload.ws', files={'file': open('test.txt')}, data=param, auth=HTTPBasicAuth('user', 'pass'))
Posts empty data as file:
r = requests.post('http://localhost/upload.ws', files={'file': open('test_äöå.txt')}, data=param, auth=HTTPBasicAuth('user', 'pass'))
try converting the string to unicode, like this:
r = requests.post('http://localhost/upload.ws', files={'file': open(u'test_äöå.txt')}, data=param, auth=HTTPBasicAuth('user', 'pass'))
or if the name is in a variable do it like:
r = requests.post('http://localhost/upload.ws', files={'file': open(filename.decode('utf8'))}, data=param, auth=HTTPBasicAuth('user', 'pass'))
its not a problem with requests library, python doesnt treat strings as unicode by default so it cant open the file ( it treats its name like ascii characters)
Requests is probably extracting the filename incorrectly from the object. Try passing an explicit filename as a unicode:
..., files={'file': (u'test_äöå.txt', open('test_äöå.txt'))}, ...
Sever.mappath is returning wrong path that is I think it is converting the initial part of the path to lowercase which is the problem;
String path = Server.MapPath("~/UploadImages/");
When I check for the path in my page by storing it in a textbox it returns:
c:\users\dell\documents\visual studio 2013\Projects\OFR\OFR\UploadImages\
instead of
C:\Users\DELL\Documents\Visual Studio 2013\Projects\OFR\OFR\UploadedImages
What can I do to get the proper path?
It is just spell mistake. you have to write like this
String path = Server.MapPath("~/UploadedImages/");
which will give your expected path
C:\Users\DELL\Documents\Visual Studio
2013\Projects\OFR\OFR\UploadedImages
Instead of UploadImages use UploadedImages.
I have following piece of code:
public void ProcessRequest (HttpContext context)
{
context.Response.ContentType = "text/rtf; charset=UTF-8";
context.Response.Charset = "UTF-8";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.AddHeader("Content-disposition", "attachment;filename=lista_obecnosci.csv");
context.Response.Write("ąęćżźń󳥌ŻŹĆŃŁÓĘ");
}
When I try to open generated csv file, I get following behavior:
In Notepad2 - everything is fine.
In Word - conversion wizard opens and asks to convert the text. It suggest UTF-8, which is somehow ok.
In Excel - I get real mess. None of those Polish characters can be displayed.
I wanted to write those special encoding-information characters in front of my string, i.e.
context.Response.Write((char)0xef);
context.Response.Write((char)0xbb);
context.Response.Write((char)0xbf);
but that won't do any good. The response stream is treating that as normal data and converts it to something different.
I'd appreciate help on this one.
I ran into the same problem, and this was my solution:
context.Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
context.Response.Write("ąęćżźń󳥌ŻŹĆŃŁÓĘ");
What you call "encoding-information" is actually a BOM. I suspect each of those "characters" is getting encoded separately. To write the BOM manually, you have to write it as three bytes, not three characters. I'm not familiar with the .NET I/O classes, but there should be a method available to you that takes a byte or byte[] parameter and writes them directly to the file.
By the way, the UTF-8 BOM is optional; in fact, its use is discouraged by the Unicode Consortium. If you don't have a specific reason for using it, save yourself some hassle and leave it out.
EDIT: I just remembered you can also write the actual BOM character, '\uFEFF', and let the encoder handle it:
context.Response.Write('\uFEFF');
I think the problem is with Excel based on Microsoft Excel mangles Diacritics in .csv files. To prove this, copy your sample output string of ąęćżźń󳥌ŻŹĆŃŁÓĘ and paste into a test file using your favorite editor, and save as a UTF-8 encoded .csv file. Open in Excel and see the same issues.
The answer from Alan Moore
translated to VB:
Context.Response.Write(""c)