你能给我一些使用文件上传来上传pdf文件的代码吗? [英] can u give me the code to upload a pdf file using file upload
本文介绍了你能给我一些使用文件上传来上传pdf文件的代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你可以给我一个上传pdf文件的代码,使用文件上传
解决方案
你的问题不完整或信息较少。它控制你使用。 />
i想给你不同的方式: -
1]使用文件上传器控制器和常规的表现: -
< span class =code-keyword>< asp:FileUpload ID = flUpld runat = server / >
< asp:RegularExpressionValidator
< span class =code-attribute> id = RegularExpressionValidator1 runat = server
ErrorMessage =只允许使用PDF文件!
ValidationExpression = ^(([A-ZA-Z] :) |?(\\ {2} \w +)\
)(\\(\\ \\ w [\ w]。*))(。pdf | .PDF)
ControlToValidate = flUpld CssClass = text-red > < / asp: RegularExpressionValidator >
2]使用ajax文件上传器控制器: -
< ajaxtoolkit:ajaxfileupload id = AjaxFileUpload1 xmlns:ajaxtoolkit = #unknown >
ThrobberID =myThrobber
ContextKeys =fred
AllowedFileTypes =jpg,pdf
MaximumNumberOfFiles = 10
runat =server/> < / ajaxtoolkit:ajaxfileupload >
3]或使用javascript脚本验证文件是。 pdf
< script type = text / javascript >
var validFilesTypes = [ PDF, PDF];
函数ValidateFile()
{
var file = document.getElementById(<% = FileUpload1.ClientID %> 跨度>);
var label = document.getElementById(<% = Label1.ClientID %> );
var path = file.value;
var ext = path.substring(path.lastIndexOf(。)+ 1,path.length).toLowerCase();
var isValidFile = false;
for(var i = 0; i < validFilesTypes.length; i ++)
{
如果 (ext = = validFilesTypes [i])
{
isValidFile = 真;
break;
}
< span class =code-attribute>
}
if (!isValidFile)
{
< span class =code-attribute> label.style.color = red ;
< span class =code-attribute>label.innerHTML = 无效文件。请上传一个文件,其中包含 +
extension:\ n \ n+ validFilesTypes.join(, );
}
return isValidFile;
}
< / script & gt;
这是简单的文件上传代码,您必须根据您的要求修改此代码
受保护 void UploadButton_Click(对象发件人,EventArgs e)
{
if (FileUploadControl.HasFile)
{
try
{
string filename = Path.GetFileName(FileUploadControl.FileName) ;
FileUploadControl.SaveAs(Server.MapPath( 〜/)+ filename);
StatusLabel.Text = 上传状态:文件已上传!;
}
catch (例外情况)
{
StatusLabel.Text = 上传状态:无法上传文件。出现以下错误: + ex.Message;
}
}
}
或者你可以使用服务器端验证和代码在这里。但我建议使用客户端验证和简单的上传代码 -
根据您的要求在下面的代码中进行更改: -
protected void btnUpload_Click( object sender,EventArgs e)
{
// 从web.config文件获取上传路径
string FilePath = ConfigurationManager.AppSettings [ FilePath 跨度>]的ToString();
bool blSucces = false ;
string filename = string .Empty;
// 检查是否选择了文件上传
if (FileUploadToServer.HasFile)
{
try
{
string [] allowdFile = { 。pdf};
// 这里我们只允许pdf文件,因此验证所选文件是否为
string FileExt = System.IO.Path.GetExtension(FileUploadToServer.PostedFile.FileName);
bool isValidFile = allowdFile.Contains(FileExt);
if (!isValidFile)
{
lblMsg.ForeColor = System.Drawing.Color.Red;
lblMsg.Text = 请仅上传pdf;
}
其他
{
// < span class =code-comment>获取上传文件的大小,此处限制文件大小
int FileSize = FileUploadToServer.PostedFile.ContentLength ;
if (FileSize < = 1048576 ) // 1048576 byte = 1MB
{
// 获取所选文件的文件名
filename = Path.GetFileName(FileUploadToServer.FileName);
// 将所选文件保存到指定位置
FileUploadToServer.SaveAs(Server。 MapPath(FilePath)+ filename);
lblMsg.Text = 文件上传成功!;
blSucces = true ;
}
else
{
lblMsg.Text = 附件文件大小不应大于1 MB!;
}
}
}
catch (例外情况)
{
lblMsg.Text = 上传文件时出错: + ex.Message;
}
}
else
{
lblMsg.Text = 请选择要上传的文件。;
}
// 将文件详细信息存储到数据库中
if (blSucces)
{
Updatefileinfo(filename,FilePath + filename);
}
}
can u give me the code to upload a pdf file using file upload
解决方案
your question is incomplete or having less information.which control you used.
i want to give you different ways:-
1]use file uploader controller with regular epression:-
<asp:FileUpload ID="flUpld" runat="server" /> <asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server" ErrorMessage="Only PDF files are allowed!" ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\
?)(\\(\w[\w].*))(.pdf|.PDF)
" ControlToValidate="flUpld" CssClass="text-red"></asp:RegularExpressionValidator>
2] Use ajax file uploader controller:-
<ajaxtoolkit:ajaxfileupload id="AjaxFileUpload1" xmlns:ajaxtoolkit="#unknown"> ThrobberID="myThrobber" ContextKeys="fred" AllowedFileTypes="jpg,pdf" MaximumNumberOfFiles=10 runat="server"/></ajaxtoolkit:ajaxfileupload>
3] or use javascript script to validate file is .pdf
<script type ="text/javascript"> var validFilesTypes=["PDF","pdf"]; function ValidateFile() { var file = document.getElementById("<%=FileUpload1.ClientID%>"); var label = document.getElementById("<%=Label1.ClientID%>"); var path = file.value; var ext=path.substring(path.lastIndexOf(".")+1,path.length).toLowerCase(); var isValidFile = false; for (var i=0; i<validFilesTypes.length; i++) { if (ext==validFilesTypes[i]) { isValidFile=true; break; } } if (!isValidFile) { label.style.color="red"; label.innerHTML="Invalid File. Please upload a File with" + " extension:\n\n"+validFilesTypes.join(", "); } return isValidFile; } </script>
this is simple file upload code you have to modify this code as per your requirement
protected void UploadButton_Click(object sender, EventArgs e) { if(FileUploadControl.HasFile) { try { string filename = Path.GetFileName(FileUploadControl.FileName); FileUploadControl.SaveAs(Server.MapPath("~/") + filename); StatusLabel.Text = "Upload status: File uploaded!"; } catch(Exception ex) { StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; } } }
or you can use server side validation and code is here. but i recommend to use client side validation and simple uploading code-
make change in below code as per your requirement:-
protected void btnUpload_Click(object sender, EventArgs e) { //Get path from web.config file to upload string FilePath = ConfigurationManager.AppSettings["FilePath"].ToString(); bool blSucces = false; string filename = string.Empty; //To check whether file is selected or not to uplaod if (FileUploadToServer.HasFile) { try { string[] allowdFile = { ".pdf" }; //Here we are allowing only pdf file so verifying selected file pdf or not string FileExt = System.IO.Path.GetExtension(FileUploadToServer.PostedFile.FileName); bool isValidFile = allowdFile.Contains(FileExt); if (!isValidFile) { lblMsg.ForeColor = System.Drawing.Color.Red; lblMsg.Text = "Please upload only pdf "; } else { // Get size of uploaded file, here restricting size of file int FileSize = FileUploadToServer.PostedFile.ContentLength; if (FileSize <= 1048576)//1048576 byte = 1MB { //Get file name of selected file filename = Path.GetFileName(FileUploadToServer.FileName); //Save selected file into specified location FileUploadToServer.SaveAs(Server.MapPath(FilePath) + filename); lblMsg.Text = "File upload successfully!"; blSucces = true; } else { lblMsg.Text = "Attachment file size should not be greater then 1 MB!"; } } } catch (Exception ex) { lblMsg.Text = "Error occurred while uploading a file: " + ex.Message; } } else { lblMsg.Text ="Please select a file to upload."; } //Store file details into database if (blSucces) { Updatefileinfo(filename, FilePath + filename); } }
这篇关于你能给我一些使用文件上传来上传pdf文件的代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文