上传图像和签名的代码及其验证 [英] Code to upload Image and signature Coupled with its Validations
本文介绍了上传图像和签名的代码及其验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,先生,
我想上传jpg格式的图像以显示照片以及签名,以验证照片和图像仅将照片和图像带到特定区域.
请尽快给我代码.帮我
Hello Sir,
I want to upload image in jpg format to display photo as well as signature besides it to validate the photo and image that it take only photo and image to particular region.
Please give me code as earlier as possible. Help me
推荐答案
您有两种选择..在客户端或服务器端进行验证.
1)从服务器端
you have two choices.. validate at client side or at server side.
1) from server side
//checks if it is an image
private bool IsImage(byte[] data)
{
//read 64 bytes of the stream only to determine the type
string myStr = System.Text.Encoding.ASCII.GetString(data).Substring(0,16);
//check if its definately an image.
if(myStr.Substring(8,2).ToString().ToLower()!="if")
{
//its not a jpeg
if(myStr.Substring(0,3).ToString().ToLower() != "gif")
{
//its not a gif
if(myStr.Substring(0,2).ToString().ToLower() != "bm")
{
//its not a .bmp
if(myStr.Substring(0,2).ToString().ToLower() != "ii")
{
//its not a tiff
//ProcessErrors("notImage");
this.myFile.PostedFile.InputStream.Close();
myStr = null;
return false;
}
}
}
}
myStr = null;
return true;
}
2)从客户端(Java脚本)
2) from client side (Java Script)
<script language="javascript">
function ValidateFile(source, args)
{
try
{
var fileAndPath=
document.getElementById(source.controltovalidate).value;
var lastPathDelimiter=fileAndPath.lastIndexOf("\\");
var fileNameOnly=fileAndPath.substring(lastPathDelimiter+1);
var file_extDelimiter=fileNameOnly.lastIndexOf(".");
var file_ext=fileNameOnly.substring(file_extDelimiter+1).toLowerCase();
if(file_ext!="jpg")
{
args.IsValid = false;
if(file_ext!="gif")
args.IsValid = false;
if(file_ext!="png")
{
args.IsValid = false;
return;
}
}
}catch(err)
{
txt="There was an error on this page.\n\n";
txt+="Error description: " + err.description + "\n\n";
txt+="Click OK to continue.\n\n";
txt+=document.getElementById(source.controltovalidate).value;
alert(txt);
}
args.IsValid = true;
}
</script>
<asp:fileupload id="FileUpload1" runat="server" xmlns:asp="#unknown" />
<asp:customvalidator id="CustomValidator1" xmlns:asp="#unknown">
ClientValidationFunction="ValidateFile" runat="server"
ControlToValidate="FileUpload1"
Display="dynamic" ErrorMessage="images only ">
如果解决您的问题,则标记为答案
mark as answer if solves your problem
这篇关于上传图像和签名的代码及其验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文