我有一个Django应用程序,提交一个包,应该返回其中的值.将表单提交给名为"insert"的视图:
request.FILES['file']
Run Code Online (Sandbox Code Playgroud)
返回文件对象,但它是一种<InMemoryUploadedFile>.我需要的是一种获取上传文件的绝对路径的方法,以便我可以将它提供给将返回所需值的方法
谁知道我怎么能做到这一点?
谢谢
例如,接下来的2个变量给出了指向同一目录的链接,但字符串是不同的.
我如何检测它们意味着相同的目录?
$dir1 = 'application/somedir/some_subdir/../';
$dir2 = 'application/somedir/';
Run Code Online (Sandbox Code Playgroud) dir = "C:\Users\Geraldes\Desktop\media\teste\ASMS_TapeA01A1691329.mxf"
print dir
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,我得到这个...我知道\ t是标签
C:\Users\Geraldes\Desktop\media (espacamento) este\ASMS_TapeA01A1691329.mxf
Run Code Online (Sandbox Code Playgroud)
但是,为了解决这个问题,我做了:
dir1 = dir.replace("\\", "\\\\")
print "dir:",dir1
Run Code Online (Sandbox Code Playgroud)
我明白了
C:\\Users\\Geraldes\\Desktop\\media (espacamento) este\\ASMS_TapeA01A1691329.mxf
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
在PHP中,为了确保路径包含适合OS的目录分隔符,可以使用DIRECTORY_SEPARATOR常量(尽管可能并不总是这样做).
Java中是否存在等效实体?
我将文件写入应用程序的文档目录:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *file = [documentsDirectory stringByAppendingPathComponent:@"data.json"];
[responseString writeToFile:file atomically:YES];
Run Code Online (Sandbox Code Playgroud)
但是如何访问该文件?
我的代码看起来像这样:
NSString *filePath = [[NSFileManager defaultManager] pathForRessorce: @"data" ofType:@"JSON"];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *data = (NSDictionary *) [parser objectWithString:fileContent error:nil];
Run Code Online (Sandbox Code Playgroud)
文件路径未填充...有人可以给我一个提示吗?
谢谢!
我需要在我的应用程序的第一次运行时从主包中复制一些png到文档文件夹.我需要它,因为我必须将这些图像的文件路径保存在核心数据实体中.
我试过这种方式,但它不起作用,有谁知道为什么?
// to copy image from main bundle to documents folder
NSString* mainBundleFilePath = [[NSBundle mainBundle] pathForResource:@"SbriciolataDiRicotta" ofType:@"png"];
NSArray *destPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [destPath objectAtIndex:0];
NSData *mainBundleFile = [NSData dataWithContentsOfFile:mainBundleFilePath];
[[NSFileManager defaultManager] createFileAtPath:documentsDirectory
contents:mainBundleFile
attributes:nil];
// to save the file path
NSString*nomeImmagine = [[NSString alloc] initWithFormat:@"SbriciolataDiRicotta"];
NSString *pngFilePath = [NSString stringWithFormat:@"%@/%@.png",documentsDirectory, nomeImmagine];
Run Code Online (Sandbox Code Playgroud)
谢谢
我有反斜杠和空格的路径,我需要作为参数发送到regedit.exe:
\\folder1\folder2\folder three\file.reg
Run Code Online (Sandbox Code Playgroud)
根据我的知识@在字符串前面使用应该允许直接指定反斜杠(没有转义).这是我正在尝试执行它的完整代码:
string path = @"\\folder1\folder2\folder three\file.reg"
Process regeditProcess = Process.Start("regedit.exe", file);
Run Code Online (Sandbox Code Playgroud)
当我尝试运行该程序时,它从regedit的输出中给出了一个错误:
无法导入\ folder1\folder2 \文件夹:打开文件时出错.可能存在磁盘或文件系统错误
由于错误报告反斜杠正确我猜测编译器或regedit在"文件夹"之后没有读取任何超过空格的内容
通过打开NTFS文件数据流时,是否可以在路径名中指定流偏移量CreateFile?
如果pathname以\\?\?开头怎么办?
例如,abcd.txt::$DATA指定未命名流*的偏移量0; 是否可以在路径名**中指定不同的偏移量?
*从技术上讲,这也意味着偏移量等于流长度,以防WriteFile附加**而不使用SetFilePointer
通过使用path/filepath包含以下示例的包,您可以从文件路径获取完整目录路径.
package main
import (
"fmt"
"path/filepath"
)
func main() {
// Output: /path/to/dir
fmt.Println(filepath.Dir("/path//to/dir/file.ext"))
}
Run Code Online (Sandbox Code Playgroud)
但是有没有从路径中Parent获取的功能dir?(这是文件目录的名称):
// The `Parent` is what I want,
// and this is a pseudo-code example, this won't actually work.
//
// Output: dir
fmt.Println(filepath.Parent("/path//to/dir/file.ext"))
Run Code Online (Sandbox Code Playgroud)
如果无法使用函数完成,如何使用RegExp获取父级的名称?
String s = "E:\Confluence_Attachments\ver003\10\85\"
+ "1835010\124\165\4915874\19169322\1";
Run Code Online (Sandbox Code Playgroud)
如何用"\"拆分上面的字符串.我尝试了以下两种方法,但两种方法都不起作用.
方法一:
String pattern = Pattern.quote(System.getProperty("file.separator"));
String[] splittedFileName = s.split(pattern);
System.out.println(" extractReqFields: ");
System.out.println(Arrays.toString(splittedFileName));
Run Code Online (Sandbox Code Playgroud)
方法2:
//String pattern2 = "\\";
//String[] splittedFileName2 = s.split(pattern);
String[] splittedFileName2 = s.split("\\");
System.out.println(" extractReqFields2222: ");
System.out.println(Arrays.toString(splittedFileName2));
Run Code Online (Sandbox Code Playgroud)
我从另一个方法获取以下字符串:
String s = "E:\Confluence_Attachments\ver003\10\85\"
+ "1835010\124\165\4915874\19169322\1";
Run Code Online (Sandbox Code Playgroud)
所以我无法摆脱反斜杠.现在我将如何用'\'拆分它?
我应该使用正则表达式以编程方式添加转义字符然后拆分吗?
你能告诉我如何以编程方式添加转义字符吗?
我能理解这里有一个答案:
但我的问题是如何以编程方式添加转义字符.我需要帮助.
filepath ×10
java ×2
path ×2
php ×2
python ×2
regex ×2
string ×2
c# ×1
createfile ×1
directory ×1
django ×1
django-views ×1
go ×1
ios ×1
mainbundle ×1
ntfs ×1
objective-c ×1
split ×1
winapi ×1