I am uploading a file with a .rvt extension. using filestream and then UploadAsync(). I can upload and download the file from a folder I generate on the Dropbox remote server using a c# desktop app called from a winform. I can also rename the file with the correct extension and it is formed correctly. below is the code to achieve that. How can I rename the file or create the file correctly on dropbox so it has the correct extension? The properly formed file is of little use without an extension ".rvt".
using (var fileStream = File.Open(localPath, FileMode.Open))
{
if (fileStream.Length <= ChunkSize)
{
await dbx.Files.UploadAsync(remotePath, WriteMode.Overwrite.Instance, body: fileStream);
}
The next issue is if I try to upload a file say 24mb or greater using the same code it returns an error?
cheers.
When you upload a file to Dropbox, it will have whatever file extension you specify, but you do need to specify it yourself.
In your code, the remotePath value should be the entire path and name, including file extension, you want for the uploaded file. For example, to upload a file "myfile" with extension "rvt" to a folder named "myfolder", the value should be "/myfolder/myfile.rvt".
The easiest way to upload a large file using C# appears to be via a WebClient object:
Asynchronous Upload of Large File Using HttpWebRequest in C#.Net
Related
I have a unique scenario in which I'd like the end result to help me upload a zip file. Here is what is happening in my workflow:
Our user is given an application on their local machine. With a click of a button, it will copy files and a zip file to remote-machine-1.
On remote-machine-2, it is running a .NET Core web app.
On remote-machine-1, I'd like to ping an endpoint off the web app in order to upload the zip file to remote-machine-2. However, the caveat is that the user will not be able to specify where this zip file is - the location of the zip file already known due to the structure of how the files and zip file are copied over in the first place.
So the question remains, with the code below - how do I pass in an IFormFile object when I call the endpoint localhost:5000/PublishTargetAsync?file=[???]? Or is there another workaround?
public async Task<bool> PublishTargetAsync(IFormFile file)
{
if (file != null)
{
using (var fileStream = new FileStream(Path.Combine(_targetOutputDirectory.ToFileSystemPath(), file.Name), FileMode.Create))
{
await file.CopyToAsync(fileStream);
}
}
return true;
}
A simple, but non optimized approach would be to use HttpClient and post the file contents as a base64 encoded string as Json using sample code similar to what is in my link. From there you could work your way back to using HttpWebRequests and a network stream and crafting the Http request by hand if necessary for performance, but the above approach should work for most small files. You'll have to modify your PublishTargetAsync endpoint to handle a post request with the right type.
I'm trying to write text to file on server from winform desktop application
string path = "http://www.site.info/doc.txt";
To use path:
System.Web.HttpContext.Current.Server.MapPath(path);
also I tried this way:
System.Web.Hosting.HostingEnvironment.MapPath(path);
to write text into the text document:
using (StreamWriter _testData = new StreamWriter(Server.MapPath("~/doc.txt"), true))
{
_testData.WriteLine("TEXT");
}
Seems like I'm doing something wrong,
name Server "does not exists in current context".
Not sure how to use Server.MapPath.
it is in References as System.Web not System.Web.dll, not sure, but it must be same, and in using as System.Web;
Also I am using System.Net; so maybe I can do it with WebClient.
You are trying to modify the file on server which the server wont allow as this could be misused and harm the server. You can update the file through the website hosting this text file.
The Server.Map path should be used in the website where you want to modify the file. If the file is asp.net web form website then you can make a aspx page that will modify file for you. If it is MVC then you will need a Action method in Controller to modify file for you.
If you want you own modified copy then you can download it and save it locally the Winform application as suggested by Sadiq. You can also upload the file by again your server side must allow this.
Why are you using Server.MapPath in a winform desktop application.
Download the file using something like this:
WebClient webClient = new WebClient();
var filearray = webClient.DownloadData(path);
and then write it to your local after modification (if needed) using
File.WriteAllBytes(savefilePath, filearray);
And then upload using webClient.UploadData(address, filearray).
I am building a web application using WCF. I need to full path of a file to open and upload it to web service.
What I am doing is first opening a file by its full path, then take stream of it and finally upload it to service. My code is below
string Path = Server.MapPath( FileUpload1.PostedFile.FileName);
System.IO.Stream fileStream = File.OpenRead(#Path);
I can't get full path of the file for security reasons.
How can I read the file that users select?
Server.MapPath(FileUpload1.FileName) is the path to a file on the server. You cannot get the full path of the client machine of the file Using the FileUpload.
There is no need for the full client path to access it. You can use the FileBytes
property in order to read the contents of uploaded file.
As others have already suggested there's no reason you need the file path to the client in order to save a file on your server. In the case you need some clearer explanation for this, then please refer to these answers:
https://stackoverflow.com/a/3077008/2196675
https://stackoverflow.com/a/1130718/2196675
I have .torrent file and I want to extract all the file names from that file. I have tried searching but i am not having enough luck.
P.S:
I am writing a simple service which will watch a specific folder to upload my torrent files to my seedbox. Then another service will poll on my seedbox to download that downloaded files. There might be some torrents which i am not intrested to upload so my utility will upload specific torrents. I want to keep log of which torrent got uploaded so that i can fetch respective downloaded folder from my seedbox.
For reference, a torrent file may call Torrent_of_movie_2010.torrent file. But when the Torrent client would download, it will download it in a folder/or a file specified in .torrent file.
here is the simple Gist for you:
https://gist.github.com/ttrider/bde3ebf5e7af6cd2b5ee
Using the .NET assembly of WinSCP to upload a file. OperationResultBase.Check() is throwing the following error:
WinSCP.SessionRemoteException: Transfer was successfully finished, but temporary transfer file 'testfile.zip.filepart' could not be renamed to target file name 'testfile.zip'. If the problem persists, you may want to turn off transfer resume support.
It seems that this happens with any zip file that I try to send. If it makes a difference, these are zip files that were created using the DotNetZip library.
Code that I'm using, taken pretty much directly from the example in the WinSCP documentation:
public void uploadFile(string filePath, string remotePath)
{
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
transferResult = currentSession.PutFiles(filePath, remotePath, false, transferOptions);
transferResult.Check();
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
}
}
Discussion over at the WinSCP forum indicates that the assembly doesn't yet allow programmatic control of transfer resume support. Is there a workaround for this?
It sounds as if the filesystem on the destination server where the file is getting uploaded to does not allow file change permissions. This could be causing the renaming of the file at the finish of the upload to fail despite the fact that the complete file was uploaded and written to the filesystem with the temporary file name used while the transfer was in progress. If you don't have administrative access to the destination server, you can test that by trying to rename a file that is already on the destination server. If that fails also, then you will either need to have the proper permissions on the destination server changed in order for that to work. Otherwise you might have to use the advice provided in your error message to turn off the resume support so it is initially opened for writing with the desired filename instead of the temporary filename (with the .filepart extension).
Turn off the resumesupport:
put *.txt -nopreservetime -nopermissions -resumesupport=off
It would help, if you included full error message, including root cause as returned by the server.
My guess is that there's an antivirus application (or similar) running on the server-side. The antivirus application checks any file once upload finishes. That conflicts with WinSCP attempt to rename the file once the upload is finished. The problem may tend to occur more frequently for .ZIP archives, either because they tend to be larger or simply because they need to get extracted before the check (what takes time).
Anyway, you can disable the transfer to temporary file name using the TransferOptions.ResumeSupport.
See also the documentation for the error message "Transfer was successfully finished, but temporary transfer file ... could not be renamed to target file name ..."
All you have to do is to disable TransferResumeSupport using the below code.
transferOptions.ResumeSupport = new TransferResumeSuppor {State = TransferResumeSupportState.Off };