接收数据时出现问题

发布于 2024-09-16 02:35:53 字数 608 浏览 4 评论 0原文

HTML:

<form enctype="multipart/form-data" action="/convert_upl" method="post">          
         Name:  <input type="text" name="file_name">
         File:  <input type="file" name="subs_file"> 
        <input type="submit" value="Send">
</form>

Python(Google App Engine):

if self.request.get('file_name'):
                    file_name = self.request.get('file_name')

我的问题是我没有从 file_name 文本输入中收到任何数据。我知道问题是因为它存在于 enctype="multipart/form-data" 形式中,但我不知道如何解决它 - 我的意思是如何一键从输入接收文件和字符串的提交按钮。

提前致谢。

HTML:

<form enctype="multipart/form-data" action="/convert_upl" method="post">          
         Name:  <input type="text" name="file_name">
         File:  <input type="file" name="subs_file"> 
        <input type="submit" value="Send">
</form>

Python (Google App Engine):

if self.request.get('file_name'):
                    file_name = self.request.get('file_name')

My problem is that I receive no data from file_name text input. I am aware that the trouble is because of it's existence within the form enctype="multipart/form-data" but I don't know how to solve it - I mean how to receive a file and the string from the input with one click of the submit button.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁绊已千年 2024-09-23 02:35:54

上传示例代码对我来说效果很好。您是否尝试过确切地使用该代码?它对您有用吗?或者您发现了什么问题?

正如您将看到的,该示例的表单与您正在使用的编码相同:

      <form action="/sign" enctype="multipart/form-data" method="post">
        <div><label>Message:</label></div>
        <div><textarea name="content" rows="3" cols="60"></textarea></div>
        <div><label>Avatar:</label></div>
        <div><input type="file" name="img"/></div>
        <div><input type="submit" value="Sign Guestbook"></div>
      </form>

只是在 HTML 中更加小心地正确使用 label 标记来显示字段标签,但这只会影响表单在浏览器中呈现时的外观。

Python 代码也与您所显示的类似(对于您所显示的小型 susbset):

def post(self):
    greeting = Greeting()
    if users.get_current_user():
        greeting.author = users.get_current_user()
    greeting.content = self.request.get("content")
    avatar = self.request.get("img")
    greeting.avatar = db.Blob(avatar)
    greeting.put()
    self.redirect('/')

当然,/sign URL 定向到其 do_post 方法的类我们刚刚展示了。

那么,如果这段代码可以工作而你的代码不能工作,那么区别在哪里呢?不在您向我们展示的部分中,所以它一定在您尚未展示的某些部分中...您可以从 Google 复制此示例代码的部分吗?工作正常吗?

The uploading example code works fine for me. Have you tried using that code exactly? Does it work for you, or what problems do you see?

As you'll see, that example has a form with the same encoding you're using:

      <form action="/sign" enctype="multipart/form-data" method="post">
        <div><label>Message:</label></div>
        <div><textarea name="content" rows="3" cols="60"></textarea></div>
        <div><label>Avatar:</label></div>
        <div><input type="file" name="img"/></div>
        <div><input type="submit" value="Sign Guestbook"></div>
      </form>

it's just a bit more careful in the HTML to properly use label tags to display field labels, but that only affect the form's looks when rendered in the browser.

The Python code is also similar to what you show (for the tiny susbset that you do show):

def post(self):
    greeting = Greeting()
    if users.get_current_user():
        greeting.author = users.get_current_user()
    greeting.content = self.request.get("content")
    avatar = self.request.get("img")
    greeting.avatar = db.Blob(avatar)
    greeting.put()
    self.redirect('/')

and of course the /sign URL is directed to the class whose do_post method we've just shown.

So, if this code works and yours doesn't, where is the difference? Not in the part you've shown us, so it must be in some parts you haven't shown... can you reproduce the part about this example code from Google working just fine?

猥琐帝 2024-09-23 02:35:54

您正在使用 POST 方法发送数据,但随后尝试使用 GET 方法获取数据。

而不是

self.request.get('file_name')

做类似的事情

self.request.post('file_name')

You are using the POST method to send the data but then are trying to get it with the GET method.

instead of

self.request.get('file_name')

do something like

self.request.post('file_name')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文