Django实现图片文字同时提交的方法

作者:NavCat 时间:2021-10-19 20:11:28 

本文实例讲述了Django实现图片文字同时提交的方法。分享给大家供大家参考。具体分析如下:

jQuery为我们网站开发解决了很多问题,使我们的网站用户体验大大的提高了。举个简单的例子,我们用AJAX技术来实现对表单的异步提交,使用户在体验上有了很大的改观,用户在提交数据的同时还可以干一些其他的事情。

不过,今天在开发中遇到一个特别头痛的问题,刚开始不知道,以为可以实现,纠结了将近4个小时之久,但结果很是令人失望。

问题是这样的:为了提高用户体验,我决定使用AJAX异步提交,于是我用jQuery的$.post去异步提交表单数据,文本信息可以很轻松的提交,但是,却怎么也无法提交图片数据。怎么办呢?

在网上查了很多资料,后来发现jQuery不支持图片上传(附件上传),但是有相关的插件,于是我开始慢慢琢磨,开始用另一个专门上传文件的插件jquery.ajaxfileupload.js,折腾了很久,总可以上传图片了。但是新的问题有产生了。

通过ajaxfileupload来异步上传图片的同时,却不能提交文本数据。囧啊…….

在网上查了很多资料,折腾了许久,没有Django开发的相关资料,怎么办?自己想办法…….

后来找到了解决方案,跟大家分享一下:

思路:

由于使用jquery.ajaxfileupload.js插件不能传递自定义的参数,肿么办?自己改写插件呗。碰巧,网上有别人改过的现成代码,二话不说,先拿来试试。以下即是我试验的过程。

1. 前台页面(部分代码):


<table border="0" width="100%">
 <form action="." method="post" id="annex_form_2"></form>
 <tbody>
 <tr>
   <td class="col_name" nowrap="nowrap" width="26%">证书名称:</td>
   <td width="64%"><input size="65" class="input_general" id="prove_name_2" maxlength="50" name="prove_name"
               type="text"></td>
   <td nowrap="nowrap" width="7%"></td>
   <td nowrap="nowrap" width="3%"><a href="javascript:void(0);" onclick="SubmitAnnexInfo(2)" title="保存"><img
       src="/static/img/hr_manage/btn_save.gif" alt="点此保存"></a></td>
 </tr>
 <tr>
   <td class="col_name">证件类型:</td>
   <td><select id="prove_type_2" name="prove_type">
     <option selected="selected" value="1">身份证</option>
     <option value="2">学位证</option>
     <option value="3">其他证</option>
   </select></td>
   <td> </td>
   <td> </td>
 </tr>
 <tr>
   <td class="col_name">证书描述:</td>
   <td><input size="80" class="input_general" id="prove_desc_2" maxlength="60" name="prove_desc" type="text"></td>
   <td> </td>
   <td> </td>
 </tr>
 <tr>
   <td class="col_name">附件地址:</td>
   <td><input size="45" class="input_general" id="prove_url_2" maxlength="45" name="prove_url" style="border:0px;"
         type="file"></td>
   <td> </td>
   <td> </td>
 </tr>
 <tr>
   <td colspan="4">
     <hr>
   </td>
 </tr>
 </tbody>
</table>

2. 更改后的jquery.ajaxfileupload.js:


jQuery.extend({
 createUploadIframe: function(id, uri)
  {
       //create frame
     var frameId = 'jUploadFrame' + id;
     if(window.ActiveXObject) {
       var io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />');
       if(typeof uri== 'boolean'){
         io.src = 'javascript:false';
       }
       else if(typeof uri== 'string'){
         io.src = uri;
       }
     }
     else {
       var io = document.createElement('iframe');
       io.id = frameId;
       io.name = frameId;
     }
     io.style.position = 'absolute';
     io.style.top = '-1000px';
     io.style.left = '-1000px';
     document.body.appendChild(io);
     return io        
 },
 createUploadForm: function(id, fileElementId, data)
  {
    //create form  
    var formId = 'jUploadForm' + id;
    var fileId = 'jUploadFile' + id;
    var form = $('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');  
    var oldElement = $('#' + fileElementId);
    var newElement = $(oldElement).clone();
    $(oldElement).attr('id', fileId);
    $(oldElement).before(newElement);
    $(oldElement).appendTo(form);
    //增加文本参数的支持
    if (data) {
       for (var i in data) {
         $('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form);
       }
    }
    //set attributes
    $(form).css('position', 'absolute');
    $(form).css('top', '-1200px');
    $(form).css('left', '-1200px');
    $(form).appendTo('body');      
    return form;
 },
 ajaxFileUpload: function(s) {
   // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout      
   s = jQuery.extend({}, jQuery.ajaxSettings, s);
   var id = new Date().getTime()    
    var form = jQuery.createUploadForm(id, s.fileElementId, s.data);
    var io = jQuery.createUploadIframe(id, s.secureuri);
    var frameId = 'jUploadFrame' + id;
    var formId = 'jUploadForm' + id;      
   // Watch for a new set of requests
   if ( s.global && ! jQuery.active++ )
    {
       jQuery.event.trigger( "ajaxStart" );
    }      
   var requestDone = false;
   // Create the request object
   var xml = {}  
   if ( s.global )
     jQuery.event.trigger("ajaxSend", [xml, s]);
   // Wait for a response to come back
   var uploadCallback = function(isTimeout)
    {        
       var io = document.getElementById(frameId);
     try
       {          
         if(io.contentWindow)
         {
            xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
          xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
         }else if(io.contentDocument)
         {
            xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
          xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
         }                
     }catch(e)
       {
         jQuery.handleError(s, xml, null, e);
       }
     if ( xml || isTimeout == "timeout")
       {          
       requestDone = true;
       var status;
       try {
         status = isTimeout != "timeout" ? "success" : "error";
         // Make sure that the request was successful or notmodified
         if ( status != "error" )
            {
           // process the data (runs the xml through httpData regardless of callback)
           var data = jQuery.uploadHttpData( xml, s.dataType );  
           // If a local callback was specified, fire it and pass it the data
           if ( s.success )
             s.success( data, status );
           // Fire the global callback
           if( s.global )
             jQuery.event.trigger( "ajaxSuccess", [xml, s] );
         } else
           jQuery.handleError(s, xml, status);
       } catch(e)
         {
         status = "error";
         jQuery.handleError(s, xml, status, e);
       }
       // The request was completed
       if( s.global )
         jQuery.event.trigger( "ajaxComplete", [xml, s] );
       // Handle the global AJAX counter
       if ( s.global && ! --jQuery.active )
         jQuery.event.trigger( "ajaxStop" );
       // Process result
       if ( s.complete )
         s.complete(xml, status);
       jQuery(io).unbind()
       setTimeout(function()
         {   try
           {
             $(io).remove();
             $(form).remove();  
           } catch(e)
           {
             jQuery.handleError(s, xml, null, e);
           }                      
         }, 100)
       xml = null
     }
   }
   // Timeout checker
   if ( s.timeout > 0 )
    {
     setTimeout(function(){
       // Check to see if the request is still happening
       if( !requestDone ) uploadCallback( "timeout" );
     }, s.timeout);
   }
   try
    {
     // var io = $('#' + frameId);
       var form = $('#' + formId);
       $(form).attr('action', s.url);
       $(form).attr('method', 'POST');
       $(form).attr('target', frameId);
     if(form.encoding)
       {
       form.encoding = 'multipart/form-data';
     }
     else
       {          
       form.enctype = 'multipart/form-data';
     }        
     $(form).submit();
   } catch(e)
    {        
     jQuery.handleError(s, xml, null, e);
   }
   if(window.attachEvent){
     document.getElementById(frameId).attachEvent('onload', uploadCallback);
   }
   else{
     document.getElementById(frameId).addEventListener('load', uploadCallback, false);
   }      
   return {abort: function () {}};  
 },
 uploadHttpData: function( r, type ) {
   var data = !type;
   data = type == "xml" || data ? r.responseXML : r.responseText;
   // If the type is "script", eval it in global context
   if ( type == "script" )
     jQuery.globalEval( data );
   // Get the JavaScript object, if JSON is used.
   if ( type == "json" )
     eval( "data = " + data );
   // evaluate scripts within html
   if ( type == "html" )
     jQuery("<div>").html(data).evalScripts();
       //alert($('param', data).each(function(){alert($(this).attr('value'));}));
   return data;
 }
})

3. 调用方法


//保存附件信息
function SaveAnnexInfo() {
 var prove_name = $("#id_prove_name").val(); //从界面得到值
 var prove_type = $("#id_prove_type").val();
 var prove_desc = $("#id_prove_desc").val();
 $.ajaxFileUpload({  
   url: "/test/annex_info /",   //请求的Url地址
   secureuri:false,  
   fileElementId:'id_prove_url',  
   dataType: 'json',  
   data: {   //加入的文本参数  
     "prove_name":prove_name,
     "prove_type":prove_type,  
     "prove_desc":prove_desc
   },  
   success: function(data) {  
     asyncbox.tips('操作成功!', 'success');          
   },  
   error: function() {  
     asyncbox.tips("上传失败,请检查文件是否符合格式要求。");  
    }  
 });  
}

4. Python后台处理(代码片段)


if annex_form.is_valid():
    annex_info = annex_form.save(commit=False)
    #获取上传
    annex_url = request.FILES.get('prove_url','') #取附件
    annex_info.entry = entry_info
    annex_info.prove_url = annex_url
    annex_info.save()
    return HttpResponse(1) #操作成功
return HttpResponse(0) #操作失败

希望本文所述对大家的Python程序设计有所帮助。

标签:Django,图片,文字
0
投稿

猜你喜欢

  • 解决MSSQL下“不能在手动或分布事务方式下创建新的连接”的问题

    2008-07-15 12:48:00
  • 一个挺酷的星级投票效果

    2010-08-03 12:44:00
  • 数据库安全管理的三个经验分享

    2009-04-01 15:49:00
  • 在ASP.NET 2.0中操作数据之三十:格式化DataList和Repeater的数据

    2023-07-22 20:15:21
  • PHP错误Warning: Cannot modify header information - headers already sent by解决方法

    2023-11-15 11:53:16
  • php mailer类调用远程SMTP服务器发送邮件实现方法

    2023-08-16 16:09:18
  • python 图片验证码代码

    2023-07-22 00:33:19
  • OBJECTPROPERTY与sp_rename更改对象名称的介绍

    2012-01-29 18:04:39
  • 关于JSON以及JSON在PHP中的应用技巧

    2023-11-16 00:03:38
  • 解读ASP.NET 5 & MVC6系列教程(17):MVC中的其他新特性

    2023-07-11 10:44:59
  • Go语言生成随机数的方法

    2023-08-28 20:11:10
  • 轻设计,让网站灵敏轻便的6个技巧

    2009-12-07 21:26:00
  • Python实现图像随机添加椒盐噪声和高斯噪声

    2023-06-13 22:54:36
  • 如何检测Oracle的ODBC是否连接成功?

    2009-11-24 20:31:00
  • 天极产品设计流程

    2007-10-11 18:47:00
  • 如何根据用户银行帐户余额的多少进行显式的提交或终止?

    2009-11-22 19:28:00
  • SQL Server 2008中的新日期数据类型

    2009-03-16 15:05:00
  • SQL Server与Oracle数据库在安全性上的异同

    2009-02-01 14:49:00
  • java往php传数据操作方法

    2023-10-27 17:51:48
  • PHP操作数组的一些函数整理介绍

    2023-11-24 14:24:17
  • asp之家 网络编程 m.aspxhome.com