I am using the REST API (C#) to upload files to Dropbox.
I am able to create a folder in Dropbox using the following API:
https://api.dropboxapi.com/1/fileops/create_folder
and able to upload a file using
https://content.dropboxapi.com/1/files_put/auto/test.jpg
How can I upload files to a particular folder?
Here is my code
var fileurl = string.Format("https://api.dropboxapi.com/1/fileops/create_folder?root=auto&path=test");
var res = await HttpClient.PostAsync(fileurl,null);
HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Length", backupContent.ContentLength.ToString());
var uploadurl = string.Format("https://content.dropboxapi.com/1/files_put/{0}?root=test", fileName);
await HttpClient.PutAsync(uploadurl , Content);
When using the Dropbox API v1 /files_put endpoint, the path parameter, which is included in the URL, should include the full path where you want to put the file, including any parent folders.
So, to upload a file named "test.txt" into a folder named "test folder", the path would be /test folder/text.txt.
You also don't actually need to explicitly create any new parent folders ahead of time. If a parent folder doesn't already exist, it will automatically be created with the upload.
Related
I need to search for filename using REST API. All files will reside in one folder.
File Name Example :
DOC_34556
DOC_35646
I will search using this name and if the file is found i need to get the URL of that file.
Try below endpoint, if file exists, it'll return FileRef, otherwise null.
/_api/web/lists/getByTitle('MyDoc3')/items?$select=FileRef&$filter=substringof(%27test2.docx%27,FileRef)
I have two projects , both are web applications. In my web application I have a upload page to allow users to upload their files. I used Server.MapPath(#"~/something/") for the directory, but how do I retrieve that file in my other application For download?
For both i use
string fileName = "SomeFile";
string filePath = Server.MapPath("~/something/") + fileName;
you may create request response page
in which
one application is request file to another one
and second application give response with file attachment
at receiver end page receive response as stream off byte and convert to specific extension.
The "~" in Server.MapPath stays for the application root and you can't use it if your upload folder is not under it. I would suggest to have a configuration key in the web.config and use it to determine the upload path.
How to: Read Application Settings from the Web.config File
StringBuilder requestUri = new StringBuilder();
requestUri.AppendFormat(
"https://apis.live.net/v5.0/me/skydrive/files?access_token={0}",
propriedades["access_token"]);
WebClient myWebClient = new WebClient();
string json = myWebClient.DownloadString(requestUri.ToString());
cloud = new JavaScriptSerializer().Deserialize<Cloud>(json);
in this Json appears only informations of folders in root, like name, id..
How can i get folder id of folders inside another folders in onedrive? Using JSON i just can see folders id of folders in root, i cannot see folders id of folders inside another folders. For example, i have a folder id in root with name "Library" and inside this folder i have two folders with names "Book1" and "Book2", how can i get the folders id of these folders?
To answer the direct question that you have, you will need to traverse the folders on your own. The API calls only retrieve for the current level of folder structure. They don't work like a Windows File system does
Additionally I agree with Fals that it might be easier to use APIs that make it nicer for you.
NOTE: Use the folder endpoint if looking at folders for ease of use.
Here is the documentation as well.
You should use the OneDrive C# API or OneDrive JavaScript API, given that you need the JSON. The API is avaible at:
Working with Microsoft OneDrive folders and files
I have used AjaxFileUpload to upload multiple image files. Now i want to store the uploaded images inside rootfolder>subfolder.
The rootfolder is in the name of the user.
The rootfolder is created dynamically by taking the session of the user who has logged in
Like this:
string username = Session["username"].ToString();
I am able to create this folder and save images in it. but i want to save it in subfolder.
the subfolder is also created dynamically but this time i have to take the value(id) from the database and name the folder by that id name. (this is so that i can refer to the database)
I know how to create a new folder using Server.MapPath();
Here is the code for it in brief
using System.IO
if (Directory.Exists(Server.MapPath(uploadPath))) return;
else Directory.CreateDirectory(Server.MapPath(uploadPath));
where uploadPath is the folder's name i want to create dynamiclly.
but how do I include the subfolder too in my Server.MapPath() so as to make my path as rootfolder/subfolder ?
Small example to make the question understandable.
I am a seller. I have posted 4 ads online.
Now when i am posting my 5th ad i want to include many images.
these images should be saved in the folder
Seller/5/imagename.jpg.
(where Seller is the username(main folder), 5 is the advertID in the database and the name of the subfolder)
How do i do this? Please help.
I am using asp.net c#
As far as I know, you can't do one statement to create folder and subfolders because you need the folder to be created first.
You have all of the code you need, you just need to repeat it. Check to see if the main folder (username) exists. If it doesn't create it, if it does, check to see if the subfolder exists. If it doesn't, create it.
Just work through that logic and you'll be set.
User Path.Combine to add your root and the user id:
var userPath = Path.Combine(uploadPath,userID)
This is the safest way to create what you need. The Directory.CreateDirectory method will create all the subfolders as needed:
var userPath = Path.Combine(uploadPath,userID)
if (Directory.Exists(Server.MapPath(userPath))) return;
else Directory.CreateDirectory(Server.MapPath(userPath));
There is a folder for file uploads on my site http://mySite/Uploads/
for each user I want to create a new folder and save files into it. It works for site visitors/ But i also have an admin site. Employees should be able to upload files with it at the same directory.
Directory.CreateDirectory returns an error, that it can't handle uri addresses.
I'm passing "http://mySite/Uploads/UserId". am i wrong? should i use another mechanisms?
am i wrong?
Yes, you should pass a physical folder to the Directory.CreateDirectory method and not an url. For example:
Directory.CreateDirectory(Server.MapPath("~/uploads/UserId"));
The Server.MapPath method should return a folder like this: c:\inetpub\wwwroot\mysite\uploads\userid.
You must use paths relative to the system root. You can do that with the MapPath method.
var uploadsRoot = Server.MapPath("~Uploads");
var userUploadFolder = Path.Combine(UploadsRoot, userId.ToString());
Directory.CreateDirecty(userUploadFolder);
If anyone interested. We made virtual folder "uploads" in IIS in both sites. Right click on your site in iis => add virtual folder or somehow, my iis isn't in english. Do it for both sites. and it's full path points at the same folder, so i can use Server.MapPath("~Uploads"); and in admin site i get http://adminSite/Uploads/, in main site it's http://mySite/Uploads/. if i upload files by one address, i allways can get access to them by another.