如何在Django中将文件发送到响应?

时间:2018-05-23 04:29:00

标签: php python django python-2.7 django-1.11

我有这样的php函数,我尝试在我的Django项目中重写。对于像inputs.recording.upload({ adapter: require('skipper-s3'), key: process.env.AWS_S3_KEY, secret: process.env.AWS_S3_SECRET, bucket: process.env.AWS_S3_BUCKETNAME }, function (err, filesUploaded) { if (err) return exits.serverError(err); console.log(filesUploaded); return exits.success(newFileRecording); }); header()这样的php方法,python中的模拟应该是什么?还有如何将文件发送到响应?

PHP:

show_error()

蟒:

function waprfile($date=false) {
    if(!isset($date) || $date==false) $date = date("d.m.y");

    $timestmp = date2timestamp($date);

    $filepath = "https://www.example.com/files/".$this->lang_code."/";

    if(file_get_contents($filepath.date("dmy",$timestmp).".xls"))
    {
        header("Location: ".$filepath."wapr".date("dmy",$timestmp).".xls");
    }
    else
    {
        show_error(_langWrite("No file for specified date", "Файл на указанную дату отсутствует"));
    }
}

1 个答案:

答案 0 :(得分:0)

要在Django中回复PDF文件,请使用以下代码。

//saves temp file instead of file name
if ($request->hasFile('imageOne')) {
            $imageOne = $request->file('imageOne');
            $filename = 'productone' . '-' . time() . '.' . $imageOne->getClientOriginalExtension();
            $location = storage_path('app/public/images/' . $filename);
            Image::make($imageOne)->resize(1200, 600)->save($location);

            if(!empty($product->imageOne)){
                Storage::delete('images/' . $product->imageOne);
            }
            $product->imageOne = $imageOne;            
        }
//works with no issue
        if ($request->hasFile('imageTwo')) {
            $imageTwo = $request->file('imageTwo');
            $filename = 'producttwo' . '-' . time() . '.' . $imageTwo->getClientOriginalExtension();
            $location = storage_path('app/public/images/' . $filename);
            Image::make($imageTwo)->resize(1200, 600)->save($location);

            if(!empty($product->imageTwo)){
                Storage::delete('images/' . $product->imageTwo);
            }
            $product->imageTwo = $filename;            
        }
//works with no issue
        if ($request->hasFile('imageThree')) {
            $imageThree = $request->file('imageThree');
            $filename = 'productthree' . '-' . time() . '.' . $imageThree->getClientOriginalExtension();
            $location = storage_path('app/public/images/' . $filename);
            Image::make($imageThree)->resize(1200, 600)->save($location);

            if(!empty($product->imageThree)){
                Storage::delete('images/' . $product->imageThree);
            }
            $product->imageThree = $filename;            
        }

1 [http://thepythondjango.com/generating-returning-pdf-response-django/]