I am trying to copy an image file from a computer on the same network as mine, but running into an exception.
I am running as aspnet core app on a Raspberry Pi. The computer with the image is an android device on the same network.
The address of the image to be copied is: ftp://172.18.10.190:8010/records/2019-07-26/157395.jpg
I can put that link into a browser and retrieve the image, but I would like to save it to the Pi.
I have tried using File.Copy:
System.IO.File.Copy("ftp://172.18.10.190:8010/records/2019-07-26/157395.jpg", directory + "/" + "157395.jpg");
I am getting a directory not found error:
NotFoundException: Could not find a part of the path 'home/pi/pcmonitor/ftp:/172.18.10.190:8010/records/2019-07-26/157395.jpg'
It looks like the 'home/pi/pcmonitor/'is being pre-pended along with one of the forward slashes after ftp being deleted.
You could not copy or download ftp file through File.Copy.
You need to send request and save the response like
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpPath);
request.Method = WebRequestMethods.Ftp.DownloadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("xx", "xx");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
using (var fileStream = File.Create(localPath))
{
responseStream.CopyTo(fileStream);
}
Console.WriteLine($"Download Complete, status {response.StatusDescription}");
response.Close();
Related
I have one file which is stored at D:/home/abc.pdf locally.
I have to read this file using AngularJs
var path="D:/home/abc.pdf";
var doc = document.createElement("a");
doc.href = path;
doc.download = path;
doc.click();
window.URL.revokeObjectURL(path);
I am not able to download this file.Giving error like Failed-Network error
That is impossible since local files are protected. Else you would be able to manipulate the hard drive as you wanted just by running a local HTML page.
So : if you want to get a file from the computer's hard drive, you have to use an <input> field and ask the user to upload the file using it.
For web browsers (such as Chrome, IE, or Firefox), when you select a file using a "Choose File" button, where does the file's data get stored?
The file name shows in the browser, but does the data of the file get stored anywhere or is just a link to the file put somewhere, such as in the browser or a temporary file?
To clarify: I want to know where the file's data get's stored BEFORE submitting. JUST after the file is selected from the client's PC an not anything else is done.
After you select a file. I believe the client (browser) just stores a reference to the file location on the user's computer. It takes a combination of js and html to post the file to the server. Via a Multi/form-data Post.
In this case, on the server, you may have to store the file to a temp location of your choosing, until you're able to process it (i.e. transform and/or store to a Datastore).
In newer browsers you can use the FormData object and xhr to post to the server which is a lot cleaner.
This FormData object is used to construct the key/value pairs which form the data payload for the xhr request.
// Create a new FormData object.
var formData = new FormData();
In this case, once the file bytes are posted to the server, you can do whatever you want with that data. Typically I'll store it as a blob in the DB.
This approach will allow you to keep it all in memory. People make the mistake of trying to store on the server file system. In some multipart form post, you might have to do it this way, however.
Here's some of my web api upload code when using XHR.
I've also called this API route using an iframe (ugh!) in order to support IE8 and older. POS browsers!
/// <summary>
/// Upload the facility logo.
/// </summary>
/// <returns></returns>
[HttpPost]
[Route("logo")]
public HttpResponseMessage Logo()
{
int newImageId = -1;
var uploadedFiles = HttpContext.Current.Request.Files;
if (uploadedFiles.Count > 0)
{
var file = uploadedFiles[0];
if (!file.IsImage())
{
// "The uploaded file must be a .jpg, .jpeg, or .png"
return
Request.CreateResponse(HttpStatusCode.UnsupportedMediaType,
"unsupported");
}
var facilityRepository = new FacilityRepository();
var logoBytes =
StreamCopier.StreamToByteArray(file.InputStream, file.ContentLength);
newImageId = facilityRepository.InsertLogoImage(logoBytes);
}
return Request.CreateResponse(HttpStatusCode.OK,
newImageId);
}
I have been trying to parse a multipart request by using apache commons file upload over JBOSS 5.1 .
The problem is when request is parsed, FileItem list is not being filled .(FileItem list is empty) Here is the code block that is working on windows but not on Unix :
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024*1024*3);
factory.setRepository(new File("/root/loads/temp"));
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(100000);
upload.setSizeMax(100000);
boolean isMulti =upload.isMultipartContent(request);
// Parse the request
try {
List<FileItem> items = upload.parseRequest(request);
Note : I am reaching the HTTPServletRequest via HttpEvent.getHTTPServletRequest().Also request has not being handled before.java version = 1.6_021
I found the solution, jboss security and our project's platform rules does not allow to access any file which are not in the specified directory.
I used jboss temp directory and can access the items in the request.
In my app I download several kinds of files (like audio,video,pdf,powerpoint,ect) .
So I receive those data from the webserver as byte[] objects.
So I would like to open them with a video player or a pdfreader now.
What am I supposed to do?
I tried this code but I guess it only works if ou have a file stored in your project's folder(that's not my case)...doesn't it?
// Access the file.
StorageFile pdfFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("FileToRead.pdf");
// Launch the pdf file.
await Windows.System.Launcher.LaunchFileAsync(pdfFile);
Use this to get a file from Isolated Storage
var file = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);
All the documentation I have found related to creating a new file and putting the new file in a user's Google Drive folder is achieved with the user uploading a file and having the python script use MediaFileUpload to gather the file and put it in Drive.
I want to create a new file in my GAE code, and put that. For example my code renders a new XML string after hitting database, and I would like to take that string, make it a file and put in Google Drive.
Anyone working with something like this?
You should use a MediaInMemoryUpload instead, which is designed for this exact purpose. You can pass a string and a MIME type.
media = MediaInMemoryUpload('some data', 'text/plain')
Use following code, content is the string you're going to put. You don't have to use MediaFileUpload and python client library.
def update(content, file_id):
url = 'https://www.googleapis.com/upload/drive/v2/files/%s?uploadType=media' % file_id
headers = {
'Content-Type': 'text/plain',
'Content-Length': str(len(content)),
'Authorization': 'Bearer <oauth2 token>'
}
response = urlfetch.fetch(url, payload=content, method='PUT', headers=headers)
assert response.status_code == 200
return response.content