Using webclient to download images from deployed website - c#

i deployed a website on IIS running on localhost/xxx/xxx.aspx . On my WPF side , i download a textfile using webclient from the localhost server and save it at my wpf app folder
this is how i do it :
protected void DownloadData(string strFileUrlToDownload)
{
WebClient client = new WebClient();
byte[] myDataBuffer = client.DownloadData(strFileUrlToDownload);
MemoryStream storeStream = new MemoryStream();
storeStream.SetLength(myDataBuffer.Length);
storeStream.Write(myDataBuffer, 0 , (int)storeStream.Length);
storeStream.Flush();
string currentpath = System.IO.Directory.GetCurrentDirectory() + #"\Folder";
using (FileStream file = new FileStream(currentpath, FileMode.Create, System.IO.FileAccess.ReadWrite))
{
byte[] bytes = new byte[storeStream.Length];
storeStream.Read(bytes, 0, (int)storeStream.Length);
file.Write(myDataBuffer, 0, (int)storeStream.Length);
storeStream.Close();
}
//The below Getstring method to get data in raw format and manipulate it as per requirement
string download = Encoding.ASCII.GetString(myDataBuffer);
}
This is by writing btyes and saving them . But how do i download multiple image files and save it on my WPF app folder? I have a URL like this localhost/websitename/folder/designs/ , under this URL , there is many images , how do i download all of them ? and save it on WPF app folder?
Basically i want to download the contents of the folder whereby the contents are actually images.

First, the WebClient class already has a method for this. Use something like client.DownloadFile(remoteUrl, localFilePath).
See this link:
WebClient.DownloadFile Method (String, String)
Secondly, you will need to index the files you want to download on the server somehow. You can't just get a directory listing over HTTP and then loop through it. The web server will need to be configured to enable directory listing, or you will need a page to generate a directory listing. Then you will need to download the results of that page as a string using WebClient.DownloadString and parse it. A simple solution would be an aspx page that outputs a plaintext list of files in the directory you want to download.
Finally, in the code you posted you're saving every single file you download as a file named "Folder". You need to generate a unique filename for each file you want to download. When you're looping through the files you want to download, use something like:
string localFilePath = Path.Combine("MyDownloadFolder", imageName);
where imageName is a unique filename (with file extension) for that file.

Related

Upload Image to Dropbox with URL

DropboxClient dbx = new DropboxClient("my_Key");
var folder = "/Apps/Images";
var file = $"fileName.jpg";
var fileToUpload = #"C:\Users\LENOVO\Test\Test\test.jpg";
using (var mem = new MemoryStream(File.ReadAllBytes(fileToUpload)))
{
var updated = await dbx.Files.UploadAsync(folder + "/" + file,
WriteMode.Overwrite.Instance,
body: mem);
Console.WriteLine("Saved {0}/{1} rev {2}", folder, file, updated.Rev);
}
i want to upload Image to Dropbox. This code is worked but i want fileToUpload to be is a web URL because images is a Web Server. i know i can download every Images step by step. But this is a loss of performance. If i write a WebUrl in the fileToUpload. i see the exception. For Example:
fileToUpload = "https:\upload.wikimedia.org\wikipedia\commons\5\51\Small_Red_Rose.JPG"
The Exception:
C:\Users\LENOVO****\bin\Debug\net6.0\https:\upload.wikimedia.org\wikipedia\commons\5\51\Small_Red_Rose.JPG
*** - is a local folder name
i want to upload image to dropbox from Web
The UploadAsync method requires that you supply the data of the file to upload directly, as you are doing in your example by retrieving the file data from the local filesystem using File.ReadAllBytes.
If you want to upload a file to Dropbox directly from a URL without downloading it to your local filesystem first, you should instead use the SaveUrlAsync method.

Download file from base64 in xamarin forms

I'm trying to download a file (that can be either image or PDF) that I've saved in my DB. I get the file in a string format, so I want to download this file. The problem is, I don't get any error, it just doesn't show the downloaded file on its folder.
Here is what I have so far.
string caminho = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string caminho_arquivo = Path.Combine(caminho, nome_arquivo);
var decoded = Encoding.UTF8.GetString(Convert.FromBase64String(arquivo.arquivo));
File.WriteAllText(caminho_arquivo, decoded);
The code below:
string caminho = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
would show the path like:
"/data/user/0/packagename/files/filename"
In Internal Storage, you couldn't see the files without root permission.
But you could use the code to check the file exist or not in the internal storage.
if (File.Exists(caminho_arquivo))
{
}
If you want to view it, you could use adb tool. Please refer to the way in the link about the adb tool.
How to write the username in a local txt file when login success and check on file for next login?

How to find a file without specific path

Im trying to send a compressed file with an email.
So the step is :
1. Compressed the file that we want to.
2. Send the email in outlook, with the attachment from the compressed file.
My problem is when the application try to search for the compressed file, it wont find because my path didnt right.
Here's the code
using (ZipFile zip = new ZipFile())
{
//zip.UseUnicodeAsNecessary = true;
zip.AddDirectory(#"Y:\"+tglskrg+"\\Result");
zip.Save(#"C:\Users\Desktop\"+tglskrg+".zip");
}
string path = Path.Combine(Directory.GetCurrentDirectory(), tglskrg + ".zip");
//Send email code(which basicly work);
my problem is , the file is saved in desktop
the actual result is , when the apps try to search for the file, the apps look in the path directory which from the code i write, the path is in the Debug folder from the application.
Anyone can help ? or maybe where did i do wrong?
Thankyou
Maybe I am not understanding your question; but, if you want to refer to the desktop folder, use Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory).
For example, you are trying to save your zip file to the desktop,
change this:
zip.Save(#"C:\Users\Desktop\"+tglskrg+".zip");
to this:
zip.Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), tglskrg + ".zip"));

Upload to Google Drive using API C# from web user

Ultimately I'm trying to upload a document from the user's file system via MVC .NET web site to Google Drive, which utilizes a service account.
I'm not sure if I'm implementing the appropriate design to accomplish the upload but I am getting hung up on the path of the file to be uploaded.
Web
#Html.TextBox("file", "file", new { type = "file", id = "fileUpload" })
Controller
public ActionResult GoogleDriveList(GoogleDrivePageVM vm, HttpPostedFileBase file)
File _file = new File();
var _uploadFile = System.IO.Path.GetFileName(file.FileName);
byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
Error occurs on the ReadAllBytes statement. It could not find file 'C:\Program Files (x86)\IIS Express\Map of Universe.txt'. The file name is correct but the path is not.
byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
... Google Drive file stuff goes here
Then upload the file from the stream.
FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, body.MimeType);
request.Upload();
So, am I going down the right path using the HTML file helper? And if so, what's the trick to get the path to work correctly? Also, I want to be able to support file sizes up to 500 MB (if that makes a difference).
If your getting the filename from a user selected windows file explorer dialog, then you shouldn't be using the below as will just strip out the filename without the path into your upload file variable and I am assuming that bogus path is where your're running the code from, so ReadAllBytes is trying to read from that path with the filename
var _uploadFile = System.IO.Path.GetFileName(file.FileName)
just change so it has that full path and filename you need to use in ReadAllBytes
var _uploadFile = file.FileName

how to download file from webserver when url contains only file ID

We have video clip storage and we want to provide url link to the end user for the clips so that end user can start downloading the clips by clicking on the url. But trick here is url link is not direct path to the clip file stored on the virtual folder but url contains clip ID. It might look like,
www.xyz.com/clipstore/ID
When end user click on the url, server should search for the clip in the clip storage, process the clip ( convert from one format to other format) and start download at end user location.
Can anyone please guide us how server can initiate download for the end user when url is not directly pointing to the file but ID of it.
We are using IIS 6 / 7 , C# on the server side. Client is silverlight based.
It is possible and quite straight forward in ASP.net MVC.
For the sake of example, I have hard coded the mime type to "image/png", but i should be according to file type.
public ActionResult clipstore(string id)
{
var path = GetFilePathByID(id);
StreamReader reader = new StreamReader(path);
var fileBytes = System.IO.File.ReadAllBytes(path);
FileContentResult file = File(fileBytes, "image/png");
return file;
}

Categories