上传照片的时候怎样转化为流保存到数据库的字段为BLOB字段中?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了上传照片的时候怎样转化为流保存到数据库的字段为BLOB字段中?相关的知识,希望对你有一定的参考价值。
就是我上传照片的时候,怎么把照片转化为流的?
string filename = this.FileUpload1.PostedFile.FileName.Substring(this.FileUpload1.PostedFile.FileName.LastIndexOf("\\") + 1);
string houzhui = FileUpload1.PostedFile.FileName.Substring(this.FileUpload1.PostedFile.FileName.LastIndexOf(".") + 1);
if (houzhui == "gif" || houzhui == "jpg" || houzhui == "bmp")
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Img/" + filename));//上传到服务器
Response.Write("<script>alert(\"上传文件成功!\");</script>");
上面是上传,在哪里处理照片转化为流呢?代码怎么写?.NET
然后在页面上<img scr='Image/<%Eval("数据库图片的字段")#>'
现在是不会把图片什么放到数据库 大家都同时访问的时候读取图片(以二进制流的方式),
希望你能看懂我的话。追问
我理解你的意思,你是把图片路径存到数据库,取的时候通过路径取出图片。但是我们现在要存档是图片,所以我要转化为流存数据库中!
参考技术A 不好意思,你能说得明白点吗,我看不明白。要么,您再请教一下其他人,好吗?实在抱歉!如何在一个表单中使用相同的上传处理来区分两个上传?
我已经为我的插件创建了一个表单,它有两个上传字段;一个用于图像,一个用于zip文件。他们都使用相同的上传处理程序,我想将附件ID保存到数据库。问题是它们使用相同的上传处理程序,因此带有附件ID的变量的值将始终是最后一个上载字段。最好的方法是怎样做的?保存在数组中(第一个索引是第一个字段,第二个索引是第二个字段)?两个上传处理程序可能有点矫枉过正。任何想法如何以一种好的方式解决这个问题?
这是处理上传的功能:
function releases_action(){
global $wpdb;
// Upload cover
$uploadfiles = $_FILES['uploadfiles'];
if (is_array($uploadfiles)) {
foreach ($uploadfiles['name'] as $key => $value) {
// look only for uploded files
if ($uploadfiles['error'][$key] == 0) {
$filetmp = $uploadfiles['tmp_name'][$key];
//clean filename and extract extension
$filename = $uploadfiles['name'][$key];
// get file info
// @fixme: wp checks the file extension....
$filetype = wp_check_filetype( basename( $filename ), null );
$filetitle = preg_replace('/\.[^.]+$/', '', basename( $filename ) );
$filename = $filetitle . '.' . $filetype['ext'];
$upload_dir = wp_upload_dir();
/**
* Check if the filename already exist in the directory and rename the
* file if necessary
*/
$i = 0;
while ( file_exists( $upload_dir['path'] .'/' . $filename ) ) {
$filename = $filetitle . '_' . $i . '.' . $filetype['ext'];
$i++;
}
$filedest = $upload_dir['path'] . '/' . $filename;
/**
* Check write permissions
*/
if ( !is_writeable( $upload_dir['path'] ) ) {
$this->msg_e('Unable to write to directory %s. Is this directory writable by the server?');
return;
}
/**
* Save temporary file to uploads dir
*/
if ( !@move_uploaded_file($filetmp, $filedest) ){
$this->msg_e("Error, the file $filetmp could not moved to : $filedest ");
continue;
}
$attachment = array(
'post_mime_type' => $filetype['type'],
'post_title' => $filetitle,
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filedest );
require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filedest );
wp_update_attachment_metadata( $attach_id, $attach_data );
}
}
}
正如我所说,由于两个上传字段都使用相同的功能,$ attach_ID变量将是最新上传的值。
function releases_action(){
global $wpdb;
// Upload cover
$uploadfiles = $_FILES['uploadfiles'];
if (is_array($uploadfiles)) {
foreach ($uploadfiles['name'] as $key => $value) {
// look only for uploded files
if ($uploadfiles['error'][$key] == 0) {
$filetmp = $uploadfiles['tmp_name'][$key];
//clean filename and extract extension
$filename = $uploadfiles['name'][$key];
// get file info
// @fixme: wp checks the file extension....
$filetype = wp_check_filetype( basename( $filename ), null );
$filetitle = preg_replace('/\.[^.]+$/', '', basename( $filename ) );
$filename = $filetitle . '.' . $filetype['ext'];
$upload_dir = wp_upload_dir();
/**
* Check if the filename already exist in the directory and rename the
* file if necessary
*/
$i = 0;
while ( file_exists( $upload_dir['path'] .'/' . $filename ) ) {
$filename = $filetitle . '_' . $i . '.' . $filetype['ext'];
$i++;
}
$filedest = $upload_dir['path'] . '/' . $filename;
/**
* Check write permissions
*/
if ( !is_writeable( $upload_dir['path'] ) ) {
$this->msg_e('Unable to write to directory %s. Is this directory writable by the server?');
return;
}
/**
* Save temporary file to uploads dir
*/
if ( !@move_uploaded_file($filetmp, $filedest) ){
$this->msg_e("Error, the file $filetmp could not moved to : $filedest ");
continue;
}
$attachment = array(
'post_mime_type' => $filetype['type'],
'post_title' => $filetitle,
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filedest );
require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filedest );
wp_update_attachment_metadata( $attach_id, $attach_data );
// $ids[]= $attach_id;
// save $attach id here, its correct for this loop, on the next loop it will be different and so on..
}
}
return $ids; // or save here serialize() maybe needed depending on how you are saving.
}
以上是关于上传照片的时候怎样转化为流保存到数据库的字段为BLOB字段中?的主要内容,如果未能解决你的问题,请参考以下文章