filenames

Saving Original File Name in Django with FileField

蓝咒 提交于 2019-12-21 02:31:07
问题 def generate_uuid_file_name(self, filename): self.original_filename = filename extension = filename.rsplit('.', 1)[1] newfilename = uuid.uuid4().__str__() + '.' + extension return self.directory() + newfilename class FileUpload(models.Model): original_filename = models.CharField(max_length=128) fileobj = models.FileField(upload_to=generate_uuid_file_name) On upload, {"errors": {"original_filename": ["This field is required."]}, "success": false} Adding blank=True, null=True to the FileUpload

Saving Original File Name in Django with FileField

Deadly 提交于 2019-12-21 02:31:07
问题 def generate_uuid_file_name(self, filename): self.original_filename = filename extension = filename.rsplit('.', 1)[1] newfilename = uuid.uuid4().__str__() + '.' + extension return self.directory() + newfilename class FileUpload(models.Model): original_filename = models.CharField(max_length=128) fileobj = models.FileField(upload_to=generate_uuid_file_name) On upload, {"errors": {"original_filename": ["This field is required."]}, "success": false} Adding blank=True, null=True to the FileUpload

Creating a thread-safe temporary file name

ε祈祈猫儿з 提交于 2019-12-20 17:38:59
问题 When using Tempfile Ruby is creating a file with a thread-safe and inter-process-safe name. I only need a file name in that way. I was wondering if there is a more straight forward approach way than: t = Tempfile.new(['fleischwurst', '.png']) temp_path = t.path t.close t.unlink 回答1: Dir::Tmpname.create You could use Dir::Tmpname.create . It figures out what temporary directory to use (unless you pass it a directory). It's a little ugly to use given that it expects a block: require 'tmpdir' #

Uses for this bash filename extraction technique?

谁都会走 提交于 2019-12-20 12:36:49
问题 I have a portion of a bash script that is getting a filename without extension, but I'm trying to understand what is really going on here. What are the "%%"'s for? Can someone elaborate on what bash is doing behind the scenes? How can this technique be used on a general basis? #!/bin/bash for src in *.tif do txt=${src%%.*} tesseract ${src} ${txt} done 回答1: It gets rid of the filename extension ( here : .tif ), sample: $ for A in test.py test.sh test.xml test.xsl; do echo "$A: ${A%%.*}"; done

bash: interpret string variable as file name/path

老子叫甜甜 提交于 2019-12-20 09:00:01
问题 My bash script receives a filename (or relative path) as a string, but must then read from that file. I can only read from a filename if I declare it as a literal directly in the script (without quotes)...which is impossible for arguments since they are implicitly strings to begin with. Observe: a="~/test.txt" #Look for it if [[ -a $a ]] ; then echo "A Found it" else echo "A Error" fi #Try to use it while read line; do echo $line done < $a b='~/test.txt' #Look for it if [[ -a $b ]] ; then

javascript url-safe filename-safe string

时光总嘲笑我的痴心妄想 提交于 2019-12-20 08:29:09
问题 Looking for a regex/replace function to take a user inputted string say, "John Smith's Cool Page" and return a filename/url safe string like "john_smith_s_cool_page.html", or something to that extent. 回答1: Well, here's one that replaces anything that's not a letter or a number, and makes it all lower case, like your example. var s = "John Smith's Cool Page"; var filename = s.replace(/[^a-z0-9]/gi, '_').toLowerCase(); Explanation: The regular expression is /[^a-z0-9]/gi . Well, actually the gi

File name has two backslashes C#

て烟熏妆下的殇ゞ 提交于 2019-12-20 03:32:22
问题 There is probably an easy answer for this, but when I added DateTime.Now.ToString() to my fileName it adds an extra \ for every \ I have so C:\Temp becomes C:\\Temp which causes the file not to save. This is the code in question String fileName = @"C:\Temp\data_" + DateTime.Now.ToString() + ".txt"; For example the output could be C:\\Temp\\data_12/04/2012 20:08:40.txt It should be C:\Temp\data_12/04/2012 20:08:40.txt 回答1: Nope, that string really has single backslashes in. Print it out to the

tkinter - retrieve file name during askopenfile

本小妞迷上赌 提交于 2019-12-20 03:20:04
问题 I have a text editor made with Python and tkinter. This is my 'open file' method: def onOpen(self): file = askopenfile(filetypes=[("Text files", "*.txt")]) txt = file.read() self.text.delete("1.0", END) root.title(file) self.text.insert(1.0, txt) file.close() I would like to set the window title equal to the file name. At the moment I'm using whatever askopenfile return as the file name, but this returns for example: <_io.TextIOWrapper name='/Users/user/Desktop/file.txt' mode='r' encoding=

How was a URL like http://stackoverflow.com/posts/1807421/edit created in PHP?

♀尐吖头ヾ 提交于 2019-12-20 02:57:11
问题 When you edit a question on stackoverflow.com, you will be redirected to a URL like this: https://stackoverflow.com/posts/1807421/edit But usually, it should be https://stackoverflow.com/posts/1807491/edit.php or https://stackoverflow.com/posts/edit.php?id=1807491 How was https://stackoverflow.com/posts/1807421/edit created? I know that Stackoverflow.com was not created by using PHP, but I am wondering how to achieve this in PHP? 回答1: With apache and PHP, you might perform one of your

Set Title of Inline File?

不羁的心 提交于 2019-12-19 19:40:28
问题 I'm forcing a download to be handled by the browser by setting the header Content-disposition to inline; Is there a way of setting the title this way? Currently it looks like the browser auto-fills it with the URL of the file. 回答1: Yes, you can specify the filename just like you'd do for content-disposition: attachment , i.e. like this: Content-disposition: inline; filename="foo.bar" See this post for some useful information about this header: How to encode the filename parameter of Content