For saving a file on the iPad, I need to get the file name substring (after sites/default/files/) in the following path string:
sites/default/files/243347_TroubleShootingSqlserver20052008.pdf
How would I do this?
Set your path to NSURL *targetURL. and then
NSString *filename = [[targetURL path] lastPathComponent];
NSArray* pathURL = [path componentsSeparatedByString: #"/"];
NSString* pdfFileName = [pathURL objectAtIndex: [pathURL length]];
Related
I have video data,
I need to convert the data into '.h264' format and save into a Temporary file.
NSString* filename = [NSString stringWithFormat:#"capture.h264"];
NSString* path = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
NSURL* url = [NSURL fileURLWithPath:path];
I have created the temporary file path but, how can I attach the data along with the file and save.
Editted:
Sorry.
I was my mistake. self answering.
NSData *data = [[NSData alloc]initWithBase64EncodedString:val options:0];
NSString* filename = [NSString stringWithFormat:#"capture.h264"];
NSString* htmlFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
[data writeToFile:htmlFilePath atomically:YES];
I am trying to retrieve PDF file using NSSearchPathForDirectoriesInDomains.i am getting path for PDF file but how to convert that pdf file into nsdata or nsmutabledata(bytes).Here is my code to get path of PDF.
NSArray *arrayPaths1 =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path1 = [arrayPaths1 objectAtIndex:0];
path1 = [path1 stringByAppendingPathComponent:#"mypdffile.pdf"];
in path1 i am getting the path and to get byte data I am trying
[decodedData writeToFile:path1 atomically:YES];
decodedData is NSMutableData.decodedData is nil.Please help me.How can I solve this problem?
Try below code..
NSData *dataPdf = [NSData dataWithContentsOfURL:pdfOnline.url];
//Get path directory
NSArray *arrayPaths1 =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
//Create PDF_Documents directory
NSString * documentsDirectory = [arrayPaths1 objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:#"mypdffile.pdf"];
[[NSFileManager defaultManager] documentsDirectory withIntermediateDirectories:YES attributes:nil error:nil];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory, #"PDFRefYouPreferred"];
[dataPdf writeToFile:filePath atomically:YES];
Hope it will fix the issue..!
You can use [NSData dataWithContentsOfFile:<#path of your file#>] for that purpose.
In your case it should be:
decodedData = [NSData dataWithContentsOfFile: path1];
Here is my code:
-(NSArray *)getSpecialArray:(NSString *)day{
NSString *stringURL = [NSString stringWithFormat:#"%#/%#%#", #"http://www.myDomain.com/", day, #".txt"];;
stringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
NSString *filePath;
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePath = [NSString stringWithFormat:#"%#/Area/%#%#", documentsDirectory, day, #".txt"];
[urlData writeToFile:filePath atomically:YES];
}
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
if(content == nil){
content = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
}
content = [content stringByReplacingOccurrencesOfString:#"Main Menu\n" withString:#""];
splitData = [content componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"\n\n"]];
return splitData;
}
What seems to be happening is that the write to file section is not working. Content seems to always == nil on that line. It also could be that it is writing just fine and it's not reading the file well for whatever reason, though I'm leaning towards the former option. Anyone see any problems with this code? Assume this is the first time running this code and the folder and file being written to doesn't exist. This problem seemed to occur right after switching to Mountain Lion and upgrading Xcode.
The problem is in creating the URL.
NSString *stringURL = [NSString stringWithFormat:#"%#/%#%#", #"http://www.myDomain.com/", day, #".txt"];
You are appending "/" in formatter as well as with URL value.
So your final URL will become http://www.myDomain.com//filename.txt. This is a invalid URL.
Correct it by removing "/" from either of place.
NSString *stringURL = [NSString stringWithFormat:#"%#%#%#", #"http://www.myDomain.com/", day, #".txt"];
At first, check if your file is actually created on filesystem and/or check returning value of [urlData writeToFile:filePath atomically:YES]; call.
If file exists and method returns YES, then the problem is in the reading data. Pass NSError** to stringWithContentsOfFile: encoding: error: and check it. Also, don't use NULL for objects, instead use nil.
I have a URL of type: http://example.com/files/
And I need to determine what file is there (extension and name of file). Depending on extensions, I'll do something.
How can I determine it?
Try
NSString *ext = [[url lastPathComponent] pathExtension];
See https://developer.apple.com/documentation/foundation/nsstring/1407801-pathextension
You can use this code directly just passing your NSUrl and get File Extension in return.
- (NSString *)findExtensionOfFileInUrl:(NSURL *)url{
NSString *urlString = [url absoluteString];
NSArray *componentsArray = [urlString componentsSeparatedByString:#"."];
NSString *fileExtension = [componentsArray lastObject];
return fileExtension;
}
Try This,
NSURL *urll = [NSURL URLWithString:#"http://example.com/files/firstFile.com"];
NSString *filenameWithExtension = [urll lastPathComponent];
NSRange *range = [filenameWithExtension rangeOfString:#"."];
USing the substringWithRange just substring of those ....
NSString *fileName = [string substringWithRange:NSMakeRange(0, range.location)];
NSString *extension = [string substringWithRange:NSMakeRange(10, string.length - range.location-1)];
NSLog(#"%# %#", fileName, extension);
How to find out and save the extension of fields that i am going to save in photo album. i want know it which format.
example:
http://www.example.com/myvideo.mp4,
http://www.example.com/mypicture.png
I need is like .mp4 and .png.If possible give me example code.
Have you looked at the documentation for NSString? You just need to send -pathExtension to your string.
If you're dealing with a string containing a URL, you should first convert it to an NSURL, then extract the path:
NSString *stringURL = #"http://...";
NSURL *url = [NSURL URLWithString:stringURL];
NSString *path = [url path];
NSString *extension = [path pathExtension];
NSURL also have pathExtension (Available in iOS 4.0 and later.)
NSString *extension = [[NSURL URLWithString: #"http://sample.example.com/path/hellowwrod.ext"] pathExtension];
refer a following code.
NSString *path = #"http://www.mysite.com/myvideo.mp4";
NSString *lastPath = [path lastPathComponent];
NSString *fileExtension = [lastPath pathExtension]; // [path pathExtension];
NSLog(#"%#", lastPath); //myvideo.mp4
NSLog(#"%#", fileExtension); // mp4
For Swift:
let stringUrl = " http://www.example.com/mypicture.png"
let url = URL(string: stringUrl)
let path = url?.path
let fileExtension = url?.pathExtension