擅长:python、mysql、java
<p>你的方法可能有几个问题:</p>
<ul>
<li>不必刷新文件内容,添加上面注释中提到的<code>f.flush()</code></li>
<li><code>NamedTemporaryFile</code>在关闭时被删除,当您退出函数时可能会发生什么,因此web服务器没有机会获取它</li>
<li>临时文件名可能超出了web服务器配置为使用<code>X-Sendfile</code>发送的路径</li>
</ul>
<p>也许最好使用<a href="https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.StreamingHttpResponse" rel="nofollow">StreamingHttpResponse</a>而不是创建临时文件和<code>X-Sendfile</code>。。。</p>