Get File Size of File to download using FTP - iphone

I am using the simpleFTPsample of apple. I want to display a progress bar of the file being downloaded. I understand it needs to be done in:
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
under the case:
case NSStreamEventOpenCompleted:
how do i retrive the file size from the NSInputStream?
I have also tried:
i have set:
[self.networkStream setProperty:#"YES" forKey:(id)kCFStreamPropertyFTPFetchResourceInfo];
and then:
NSLog(#"size: %#",[self.networkStream propertyForKey:(id)kCFStreamPropertyFTPResourceSize]);
but the result is null...

You will have to set kCFStreamPropertyFTPFetchResourceInfo to true. That way the CFFTPStream will send a STAT command to the FTP server to get file info, including the total size.

To get the file size you just need:
case NSStreamEventOpenCompleted: {
fileSize = [[self.networkStream propertyForKey:(id)kCFStreamPropertyFTPResourceSize] integerValue];}
By the way do you know how to get the modification date of the file in the ftp server??

Related

Specify file offset when creating NSData with contentsOfFile arguments

I am trying to implement a file uploader using NSURLSession.
The file to be uploaded is specified as:
var data: NSData = NSData(contentsOfFile: path)
Suppose, the upload fails for some reason & assuming that I can get an offset to resume upload from.
Is it possible to specify in NSData that we have to start from a given offset (something like do a seek before doing the upload)?
There's a class named NSFileHandle. Personally I never used it but it seems like it does what you need. It has seekToFileOffset method and availableData property so I think you can try it

How can I use FinishUpload to close a chunked file upload when I have 0 bytes left?

I'm uploading files, in chunks, to SharePoint Online through the REST API.
I am doing this via the StartUpload, ContinueUpload and FinishUpload methods and passing them a chunk as a byte[].
I have used this as the example to work from: Using chunked upload/StartUpload with sharepoint REST api
Documentation is here: https://msdn.microsoft.com/library/office/microsoft.sharepoint.client.file.startupload.aspx
(I have not included the code as I don't think it's relevant, please correct me if I'm mistaken)
This works as long as the total file size is larger than the chunk size.
For example, if the chunk size is 1MB, but the total file size is 4MB, then the method will work.
If the chunk size is 4MB, but the total file size is 1MB, then I end up with empty or corrupt files once uploaded.
This is because the initial call to StartUpload contains the entire file in one chunk, so FinishUpload never gets called to close the file.
If I call FinishUpload with an empty byte[0] then I get:
Error 500 - Internal Server Error: The upload was incomplete. Try to save again.
Under normal circumstances I would simply check if the total file size was smaller than the chunk size and instead add the file directly using /Files/add(...).
Unfortunately I am being passed the file chunks in a stream and I don't know the total file size beforehand. I also can't save all chunks before processing, I have to pass each chunk straight to SharePoint as I am given them.
So how do I use FinishUpload to close a file upload when I have no more bytes to upload?
For that matter you could consider the following modifications:
1) in case if total file size is less then chunk size, then the file
is getting uploaded via a single request. For example:
var fi = new FileInfo(fileName);
if(fi.Length <= chunkSize)
{
this.UploadFile(address, fileName);
return;
}
where WebClient.UploadFile Method is used for uploading a file via SharePoint RPC
2) otherwise file is getting uploaded via chunk session
Refer SharePointClient.cs for a complete example.

iPhone how to compress the files while upload to server?

What is the best way to compress and upload file from iPhone? Am using ASIHTTP to upload the file to my server. But am getting network error while upload the large files to server. So how to compress and upload it using ASIHTTP?
Code:
[serverUploadRequest setPostValue:withEmail_id forKey:#"mail_id"];
[serverUploadRequest setPostValue:withPassword forKey:#"pwd"];
[serverUploadRequest setPostValue:withFileName forKey:#"file_name"];
[serverUploadRequest setPostValue:withFileExtension forKey:#"file_extension"];
[serverUploadRequest setData:fileData withFileName:withFileName andContentType:#"application/octet-stream" forKey:#"userfile"];
[serverUploadRequest startSynchronous];
This is working fine but when i try to upload the large files some times am getting upload failed error. So is possible to compress and upload the file to server. Am using windows 2003 server with PHP
Thanks
Use Objective-Zip to zip the file.
For more info about its usage, visit this link
May this link useful to you
You can zip file you files before uploading to server.
easy & fast C based zip.
http://code.google.com/p/miniz/source/browse/trunk/miniz.c?r=31
Here is a simple method I used for creating a zip to upload 3 image.
-(void)zipImage:(NSString *) filename: (NSString *) file1: (NSString *) file2: (NSString *) file3{
ZipArchive *zipfile = [[ZipArchive alloc]init];
[zipfile CreateZipFile2:filename];
[zipfile addFileToZip:file1 newname:#"C1.jpg"];
[zipfile addFileToZip:file2 newname:#"C2.jpg"];
[zipfile addFileToZip:file3 newname:#"T1.jpg"];
[zipfile CloseZipFile2];
}
The zip library is miniZip ( http://code.google.com/p/ziparchive/ )
As you can see the initial call is to init the ZipArchive object, next create the zipfile, then just recursively call addFileToZip for each item you want in your file.
P.

How to Check if an image is valid on iPhone

How to Check if an image is valid
For example, when I cached an image which was downloaded only half and failed, and this image is invalid, then I want to know it's an invalid image and download again.
(When I use the broken image, Xcode console logs an error:
ImageIO: PNG IDAT: CRC error )
So I want find a mechanism to check image's validation
for different kinds of JPEG,PNG,etc
anybody has some clues?
I would start by checking for valid header.
Secondly the footer. Usually the last 8 bytes uint values in order are
'73,69,78,68,174,66,96,130'.
This converted into a Int64 equals 5279712195050102914
This should do for a png :)
if (memcmp(img_bytes, "\211PNG", 4) != 0||OSReadBigInt64(img_bytes,(length - 8))!=5279712195050102914)
{
//Bad Data! Free your data and return or something
}
It happens because you are using same class to download image file from server and you request consequent download of same file, while it is already downloading that file, which fails the earlier one file download and results in partly downloaded file.
I suggest you to get the file size of the image & compare with the image file just downloaded.
For loading images from the network there are two obvious failure cases:
The downloaded file was not an image at all.
The downloaded data only contains part of the image data.
For the first case nil checking the return value of various UIImage methods is probably the best way to handle this error case.
The following class methods on UIImage will return nil if the image could not be initialized with the data it was provided: + (UIImage *)imageWithData:(NSData *)data and + (UIImage *)imageWithContentsOfFile:(NSString *)path.
Code that does the nil check might look like this:
UIImage *image = [UIImage imageWithContentsOfFile:pathToImage];
if (image) {
// The image was loaded. Display it
}
else {
// The image was not loaded. Redownload or display a placeholder, etc...
}
For the second failure case it is likely the result of lost connection. For this case the failure is best handled in the NSURLConnection's delegate. When downloading the image if - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error is called then the data for the request should be discarded.
Also keep in mind that didFailWithError: will not be called for 4xx or 5xx responses from the server. It is called when the connection is cut off for other reasons. It would generally be a good idea to make sure you only write image data to disk when the server has responded with a 200 OK.

SWFUpload + jQuery.SWFUpload - Remove File From Queue

I'm facing a big issue IMO.
First, here's my code:
.bind('uploadSuccess', function(event, file, serverData){
if(serverData === 'nofile') {
var swfu = $.swfupload.getInstance('#form');
swfu.cancelUpload(file.id); // This part is not working :(
} else {
alert('File uploaded');
}
})
In this part I'm checking server response (I'm have strict validation restrictions). Now my question. Is it possible to remove uploaded file from queue? Basically, if server returns error I display error message, but... this file still exsit in the queue (I've implemented checking filename and filesize to avoid duplicated uploads) and user is not possible to replace this file (due to upload and queue limit).
I was trying to search for a solution, but without success. Any ideas?
Regards,
Tom
From the link
http://swfupload.org/forum/generaldiscussion/881
"The cancelUpload(file_id) function
allows you to cancel any file you have
queued.
You just have to keep the file's ID
value so you can pass it to
cancelUpload when you call it."
Probably you have to keep the file ID before sending anything to the server