Download file from dropbox failing when name contains spaces - c#

I am trying to download a file from dropbox using the RESTful API. When I encounter a file that has a space in it "My Photo.png" the program stops on var request = (HttpWebRequest) WebRequest.Create(requestUri); It returns a 403 error. If I remove the spaces and try the file download again it works perfectly. Ive checked the formatted uri and it is being returned as "My+Photo.png" is this how it should be? What am I doing wrong?
var uri = new Uri(new Uri(DropboxRestApi.ApiContentServer),
String.Format("files?root={0}&path={1}",
root, UpperCaseUrlEncode(path)));
My Method:
private static string UpperCaseUrlEncode(string s)
{
char[] temp = HttpUtility.UrlEncode(s).ToCharArray();
for (int i = 0; i < temp.Length - 2; i++)
{
if (temp[i] == '%')
{
temp[i + 1] = char.ToUpper(temp[i + 1]);
temp[i + 2] = char.ToUpper(temp[i + 2]);
}
}
return new string(temp);
}

Could you do the normal URLEncode and do a string.Replace on the temp string it will work perfect for files with a space in the file name
temp = temp.Replace("+", "%20");

Related

While loop int does not update

I have a script that scrapes a website, but the while loop does not work. The script downloads a website and then checks if it is a website or a image.
If the downloaded item is a HTML file it saves it and adds 1 to i and the URL.
Problem: The URL does not change, even tho I think it should with this code.
int i = 0;
while (i < 5)
{
using var client = new WebClient();
client.Headers.Add("User-Agent", "C# console program");
int urlnumb = 1;
string url = "http://localhost:7211/database/resource/pk/" + urlnumb;
string content = client.DownloadString(url);
string htmldefiner = "html";
if (content.Contains(htmldefiner))
{
string savedirectory = #"C:/Temp/" + i + ".html";
System.IO.File.WriteAllText(savedirectory, content);
i++;
urlnumb++;
File.WriteAllText(#"C:/Temp/" + i + ".txt", url);
}
else
{
urlnumb++;
}
}

how to output the child node from a xml file using C# into csv file

I have an application that outputs an CSV file. In the project I have a xml config file that contain a url.
<url>http://localhost/test</url>
What I am trying to achieve its to output into the csv file the attribute form the xml file (example http://localhost/test).
I need to output this into the file.WriteLine line code but I don't know how to output the content of the url inside the xml file.
I have created a XmlTextReader but I can't still output the url
Here you have part of my code:
int count = xml.GetElementsByTagName("url").Count;
for (int i = 0; i < count; ++i)
{
url.Add(xml.GetElementsByTagName("url")[i].InnerText);
}
XmlTextReader reader = new XmlTextReader("config.xml");
using (StreamWriter file = new StreamWriter(fs))
{
file.WriteLine("ProjectID, ProjectTitle,PublishStatus,Length");
for (int s = 0; s < pr.Length; ++s)
{
string[] UsersIDS = new string[] {""};
UsersIDS = db.GetUsersList(pr[s].ProjectID);
file.WriteLine( pr[s].ProjectID + '"' + ',' + '"' + pr[s].ProjectTitle + '"' + ',' + pr[s].PublishStatus + '"' + ',' + UsersIDS.Length);
}//end of for
After testing I got it work by adding the following:
url[i].ToString()
Thanks anyways

German special characters Ää, Öö, Üü, ß are displaying incorrectly in LiveLink or opentext

I have Document and folder in Live link containing German special characters Ää, Öö, Üü, ß. I extracted the document and folder name using following code of LiveLink API server version 9.2.0.0 and client version API version of LiveLink 9.5.0.0.
string encodedName = LLValueUtil.GetValue(mainLLObj, "Name").TrimEnd('.');
int charIndex = 0;
while (Array.IndexOf(WhitespaceChars, encodedName[charIndex]) >= 0) {
string replacement = System.Xml.XmlConvert.EncodeName(encodedName[charIndex].ToString());
encodedName = encodedName.Substring(0, charIndex) + replacement + encodedName.Substring(charIndex + 1);
charIndex += replacement.Length;
}
charIndex = encodedName.Length - 1;
// Replaces trailing WhitespaceChars
while (Array.IndexOf(WhitespaceChars, encodedName[charIndex]) >= 0) {
string replacement = System.Xml.XmlConvert.EncodeName(encodedName[charIndex].ToString());
string lastPart = encodedName.Substring(charIndex + 1);
encodedName = encodedName.Substring(0, charIndex) + replacement + lastPart;
charIndex = encodedName.Length - replacement.Length - lastPart.Length - 1;
}
string documentName = encodedName; // give fine File name
this code works fine in Livelink sever API version 9.5.0.0. but does not work in LiveLink API server version 9.7.1. Could you help me fixing for this issue ?
Go to admin panel of server Livelink/livelink.exe?func=admin.sysvars and set Character Set: UTF-8 and code section change as follow
byte[] bytes = Encoding.Default.GetBytes(value);
var retValue = Encoding.UTF8.GetString(bytes);

Split file from a url

I had some codes to split files and join ,Now am trying to make a program that split file from a url and download , for example http://tegos.ru/new/mp3_full/David_Guetta_feat_Ne-Yo_and_Akon_-_Play_Hard.mp3 spilit this file in two pieces and download
my split-er code
Byte[] byteSource = System.IO.File.ReadAllBytes(FileInputpath);
FileInfo fiSource = new FileInfo(txtPath.Text);
int partsize = (int)Math.Ceiling((double)(fiSource.Length / OutputFiles));
int fileOffset = 0;
string currPartPath;
FileStream fsPart;
int sizeReamining = (int)fiSource.Length;
for (int i = 0; i < OutputFiles; i++)
{
currPartPath = FolderOutputPath + "\\" + fiSource.Name + "." + String.Format(#"{0:D4}", i) + ".gparts";
if (!File.Exists(currPartPath))
{
fsPart = new FileStream(currPartPath, FileMode.CreateNew);
sizeReamining = (int)fiSource.Length - (i * partsize);
if (sizeReamining < partsize)
{
partsize = sizeReamining;
}
fsPart.Write(byteSource, fileOffset, partsize);
fsPart.Close();
fileOffset += partsize;
}
}
Use HttpWebRequest to get a stream from the specified url.
When you have the stream, you can use your previous code.
Download/Stream file from URL - asp.net

How to check parent folder

I have get my website path using HttpRuntime.AppDomainAppPath (like this C:/personal/Website/page.aspx)
Web service is always located on page.aspx parent of parent folder (like this C:/personal/Service/service.asmx ). I get the webservice-path using a ABC.dll in servicePath variable like this string servicePath="C:/personal/Service/service.asmx".
How to check service path against website path?
If (GetWebPath()== GetServicePath())
{
// ... do something
}
private string GetWebPath()
{
string path = HttpRuntime.AppDomainAppPath;
string[] array = path.Split('\\');
string removeString = "";
for(int i = array.Length; --i >= 0; )
{
removeString = array[array.Length - 2];
break;
}
path = path.Replace(#"\" + removeString + #"\", "");
return path;
}
private string GetServicePath()
{
string path = #"C:\MNJ\OLK\ABC.asmx"
string[] array = path.Split('\\');
string removeString = "";
for(int i = array.Length; --i >= 0; )
{
removeString = #"\" + array[array.Length - 2] + #"\" + array[array.Length - 1];
path = path.Replace(removeString, "");
break;
}
return path;
}
string webPath = #"C:\blabla\CS_Web\website\";
string servicePath = #"C:\blabla\CS_Web\SPM\Server.asmx";
if(Path.GetDirectory(Path.GetDirectoryName(servicePath))==Path.GetDirectoryName(webPath)
{
//You do something here
}
You have to up page to parent folder using Path.GetDirectoryName()
Try this:
System.Web.Server.MapPath(webPath);
This will return the physical file path of the currently executing web file.
More information can be found here: System.Web.Server
Providing you want to check the following pathes:
string webPath = #"C:\blabla\CS_Web\website\";
string servicePath = #"C:\blabla\CS_Web\SPM\Server.asmx";
you should call
string webPathParentDir = GetParentDirectoryName(webPath);
string servicePathParentDir = GetParentDirectoryName(servicePath);
if (servicePathParentDir.Equals(webPathParentDir, StringComparison.OrdinalIgnoreCase))
{
// ... do something
}
with method:
private string GetParentDirectoryName(string path)
{
string pathDirectory = Path.GetDirectoryName(servicePath);
return new DirectoryInfo(pathDirectory).Parent.FullName;
}

Categories

Resources