我正在使用Django的管理站点来管理一些数据 - 但是没有构建webapp.我需要文件上传字段来仅存储数据库中的文件名.
现在我可以得到像/Users/me/uploads/file.png这样的绝对路径
或者使用upload_to参数在数据库中获取类似内容:uploads/file.png
我怎么能得到它:file.png
编辑:
我正在使用sqlite数据库并在另一个客户端中使用 - 因此在我的情况下,db条目中的任何路径都没有意义.
谢谢!
我一直对此感到好奇,当使用数组内部的结构时,就内存分配而言,为数组中的每个条目分配新结构是否更好,或者更好地分配足够的空间在N个结构的数组中.
//pointer based:
struct myStructure ** tmp = malloc(sizeof(struct myStructure *) * N);
tmp[0] = malloc(sizeof(struct myStructure));
tmp[0]->whatever = true;
//or structure in the array:
struct myStructure * tmp = malloc(sizeof(struct myStructure) * N);
tmp[0].whatever = true
Run Code Online (Sandbox Code Playgroud)
是否有任何好处超过一个或另一个?我觉得使用第二种形式是更好的练习,因为你最终会减少较小的malloc调用,但可能会出现只能使用第一种方法的情况.
对此有何见解?
谢谢!
我很难找到有关如何处理触摸事件以支持与键盘类似的行为的正确文档。
我想要的是一个按钮,当我长按它时,它会在按钮上方显示一个自定义视图控制器,但我希望用户能够将手指拖动到其他按钮之一(无需将手指离开屏幕) 。
我长按按钮,它是自定义视图控制器的所有设置和工作。我不知道如何支持从视图控制器中的第一个按钮拖动到另一个按钮以便能够选择它。
我尝试使用子类 UIButton 尝试这样做:
[self addTarget:self action:@selector(onDragOver:) forControlEvents:UIControlEventTouchDragEnter];
但这是行不通的。
我还发现了这个问题How to track Button Selection after long press? 这正是我想要复制的功能。但没有答案。
我遇到了一个问题,我不确定是否是来自kqueue的预期行为,或者是否是我做错的事情.
我需要使用kqueue为单个套接字文件描述符安装单独的事件.单独的事件是(sockfd,EVFILT_READ)和(sockfd,EVFILT_WRITE).然而,当我从kqueue中拉出事件后,我从kqueue获得两个事件,但两个事件都包含(event.filter&EVFILT_READ)和(event.filter&EVFILT_WRITE).
我需要将它们分开,否则我不知道哪个事件实际上是准备好的,可读的还是写的?
以下是我用一些示例代码来说明问题.请记住,这只是测试代码,以确定为什么我在每个事件中都得到两个过滤器标志.
int main(int argc, char ** argv) {
//listening_sockfd = setup listening socket
//setup kqueue and vars
int kq = kqueue();
int res = 0;
int bufres = 0;
struct timespec ts = {0};
struct timespec * tsp = &ts;
struct kevent ke2[2];
//install read for listening socket
struct kevent ke1;
bzero(&ke1,sizeof(ke1));
ke1.ident = listening_sockfd;
ke1.flags = EV_ADD;
ke1.filter = EVFILT_READ;
res = kevent(kq,&ke1,1,NULL,0,tsp);
while(1) {
new_client:
//wait for a client to connect
res = kevent(kq,NULL,0,&ke1,1,NULL);
if(res …
Run Code Online (Sandbox Code Playgroud) 我正在与NSWindowController一起实现首选项窗口。Apple的文档指出,默认情况下不会释放控制器和窗口,因为不必重新加载所有内容非常有用,这很有意义。但是他们的文档继续说您可以覆盖该行为,但不解释如何做。
苹果的文档:
When a window is closed and it is part of a document-based
application, the document removes the window’s window
controller from its list of window controllers. This results
in the system deallocating the window controller and the
window, and possibly the NSDocument object itself. When a
window controller is not part of a document-based application,
closing the window does not by default result in the
deallocation of the window or window controller. This is the
desired behavior for …
Run Code Online (Sandbox Code Playgroud) 我正在开发一个下载zip文件并在本地解压缩的项目.我遇到的问题是解压缩过程在5%的时间内起作用.
在这一点上,这对我来说是一个谜,因为有时它可以工作,但大多数时候它会抛出数据或crc错误.即使zip文件没有改变,它甚至会在错误之间切换.
我尝试过由众多工具创建的zip文件,想知道格式是否不正确.但无济于事.即使在终端中创建的拉链也不起作用.
这是我的解压缩代码:
try {
String _location = model.getLocalPath();
FileInputStream fin = new FileInputStream(localFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
byte[] buffer = new byte[1024];
while((ze = zin.getNextEntry()) != null) {
if(_cancel) break;
System.out.println("unzipping " + ze.getName());
if(ze.isDirectory()) {
File f = new File(_location + ze.getName());
f.mkdirs();
} else {
FileOutputStream fout = new FileOutputStream(_location + ze.getName());
for(int c = zin.read(buffer); c > 0; c = zin.read(buffer)) {
fout.write(buffer,0,c);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
if(_cancel) …
Run Code Online (Sandbox Code Playgroud) 我正在研究一个简单的原型,需要测试从一个设备向另一个设备发送推送通知.
我已通过电子邮件发送Urban Airship为我的应用程序打开"允许从设备推送" - 他们确实打开了它.
我正在尝试使用NSURLConnection从设备发送推送通知.
这是我的代码:
- (void) test {
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://go.urbanairship.com/api/push"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSDictionary * push = @{@"device_tokens":@[@"<token>"], @"aps":@{@"alert":@"TEST", @"sound":@"default"}};
NSData * pushdata = [NSJSONSerialization dataWithJSONObject:push options:0 error:NULL];
[request setHTTPBody:pushdata];
[NSURLConnection connectionWithRequest:request delegate:self];
}
- (void) connection:(NSURLConnection *) connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *) challenge {
if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic]) {
NSURLCredential * credential = [[NSURLCredential alloc] initWithUser:@"<app key>" password:@"<app secret>" persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
[credential release];
}
}
- (void) connection:(NSURLConnection *) connection …
Run Code Online (Sandbox Code Playgroud)