在ASP.net上传pdf文件 [英] Upload pdf files in ASP.net
本文介绍了在ASP.net上传pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的所有人,
我需要上传PDF文件和能够查看文件的用户。
请给我建议我必须保存/存储文件,即在数据库或服务器应用程序文件夹中。
我需要上传200每季度的文件。文件大小的平均值为1 MB。
我希望你能理解这一点。
问候
Sheik
Dear All,
I have the requirement to upload the PDF files and the users able to view the files.
Please give me the suggestion where I have to keep/store the files i.e in the database or server application folder.
I need to upload 200 files per quarter. The average of the file size is 1 MB.
I hope you understand this.
Regards
Sheik
推荐答案
把它放在文件夹中并将URL保存在数据库中
put this in folder and save url in database as
protected void btnSub_Click(object sender, EventArgs e)
{
try
{
string filename = FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(Server.MapPath("~\\Uploadform\\" + filename.Trim()));
string path = "~\\Uploadform\\" + filename.Trim();
SqlConnection con = new SqlConnection(str);
cmd = new SqlCommand("Insert into Circular(date,ctype,title,type,url1,url2) values('"+txtdate.Text+"','" + TextBox1.Text + "','" + txttitle.Text + "','" + txttype.Text + "','" + path + "')", con);
lblinfo.Text = " Uploaded Successfully ";
cmd.CommandType = CommandType.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
你可以通过以下方式做到这一点..
u can do this by following way..
protected void upload_Click(object sender, EventArgs e)
{
Label2.Visible = true;
string filePath = FileUpload1.PostedFile.FileName; // getting the file path of uploaded file
string filename1 = Path.GetFileName(filePath); // getting the file name of uploaded file
string ext = Path.GetExtension(filename1); // getting the file extension of uploaded file
string type = String.Empty;
if (!FileUpload1.HasFile)
{
Label2.Text = "Please Select File"; //if file uploader has no file selected
}
else
if (FileUpload1.HasFile)
{
try
{
switch (ext) // this switch code validate the files which allow to upload only PDF file
{
case ".pdf":
type = "application/pdf";
break;
}
if (type != String.Empty)
{
connection();
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs); //reads the binary files
Byte[] bytes = br.ReadBytes((Int32)fs.Length); //counting the file length into bytes
query = "insert into PDFFiles (Name,type,data)" + " values (@Name, @type, @Data)"; //insert query
com = new SqlCommand(query, con);
com.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename1;
com.Parameters.Add("@type", SqlDbType.VarChar).Value = type;
com.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
com.ExecuteNonQuery();
Label2.ForeColor = System.Drawing.Color.Green;
Label2.Text = "File Uploaded Successfully";
}
else
{
Label2.ForeColor = System.Drawing.Color.Red;
Label2.Text = "Select Only PDF Files "; // if file is other than speified extension
}
}
catch (Exception ex)
{
Label2.Text = "Error: " + ex.Message.ToString();
}
}
}
//Add the following code in the view file button click to View uploaded PDF files in GridView
protected void Button2_Click(object sender, EventArgs e)
{
connection();
query = "Select *from PDFFiles";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
//Add the following code to the Gridview selected index changed event to download the files:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
connection();
SqlCommand com =new SqlCommand("select Name,type,data from PDFFiles where id=@id", con);
com.Parameters.AddWithValue("id", GridView1.SelectedRow.Cells[1].Text);
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
{
Response.Clear();
Response.Buffer =true;
Response.ContentType = dr["type"].ToString();
Response.AddHeader("content-disposition", "attachment;filename=" + dr["Name"].ToString()); // to open file prompt Box open or Save file
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite((byte[])dr["data"]);
Response.End();
}
}
您好,
您可以将它放在应用程序文件夹中。当您需要查看时,只需将超链接指向pdf文档的特定路径。
谢谢。
Hi,
You can put it in application folder.When you need to view this, just give the hyperlink to the particular path of pdf document.
Thanks.
这篇关于在ASP.net上传pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文