The correct method for obtaining the Upload File name through web. py, and web. py for obtaining the Upload File Name

Source: Internet
Author: User

The correct method for obtaining the Upload File name through web. py, and web. py for obtaining the Upload File Name

Directly cut into the topic, upload files from the HTML page, and Python receives and processes the files. But I found some minor problems. Writing it out is a matter of accumulation!

HTML page code:

Copy codeThe Code is as follows:
<Form action = "/admin/addgoodsaction/" method = "post" enctype = "multipart/form-data">
<Input type = "file" name = "image"/>
</Form>

Python processing code:
Copy codeThe Code is as follows:
I = web. input ()
Return I. image. filename

The execution result switch prompt is as follows:
Copy codeThe Code is as follows:
<Type 'exceptions. attributeerror'> at/admin/addgoodsaction/
'Str' object has no attribute 'filename'

The system prompts that this object does not find the filename attribute. At first, I thought it was an error to receive the object, so I directly
Copy codeThe Code is as follows:
I = web. input ()
Return I. image

The uploaded image can be printed normally. It proves that there is no error in the receiving object. Why is the received object correct, but the system prompts that there is no filename attribute?
I changed the code:
Copy codeThe Code is as follows:
I = web. input (image = {})
Return I. image. filename

Upload a hello.jpg image and print hello.jpg. The problem is solved. From the code above, if the image object is not initialized, the filename attribute cannot be found. After initialization, the filename attribute can be obtained to obtain the uploaded file name.


How does JAVA obtain the uploaded file name?

I agree to the analysis on the upstairs. I also want to provide a suggestion. I can try to write the code step by step for ease of reading.
Define filename and filepath first, and then you want the image to appear in the specified area in subsequent operations.
You can directly use filename. The file name to be uploaded is its. getIPTimeStampRand () + "." + item. getName (). split ("\.") [1].

Get Upload File Name

Send the code -------- or you can find an asp upload component. All problems are solved.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.