将文件上传到目录 [英] Uploading files to directory
本文介绍了将文件上传到目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我当前正在运行一个应用程序,在该应用程序中,我需要用户能够将文件上传到服务器上的目录.
请帮助
Hi
I currently am running an application in which i need users to be able to upload a file to a directory on the server.
please help
推荐答案
添加 ^ ]添加到您的页面.该链接包含一个可以满足您需求的示例.
Add a FileUpload control[^] to your page. The link includes an example that does what you want.
Hii,我认为这对您有所帮助.我希望它能用C#理解..
Hii, I think it helps to u. It is in C# i hopes u can understand..
if (UploadImage.PostedFile != null)
{
string fileName = UploadText.FileName.ToString();
string[] dots = fileName.Split('.');
string fileType = "txt";
string type = dots[dots.Length - 1];
string type1 = dots[dots.Length - 1];
if (fileType.IndexOf(type) == -1)
{
UploadText.Focus();
return;
}
else
{
string UploadDescription = Description.Content;
string strUploadPath = "", strFilePath = "";
strFilePath = @"C:\\UploadTextFiles\";
string path = DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Millisecond.ToString();
strUploadPath = Server.MapPath(strFilePath) + path + UploadText.FileName;
UploadText.PostedFile.SaveAs(strUploadPath);
// UploadText.SaveAs(strUploadPath);
byte[] imageSize = new byte[UploadImage.PostedFile.ContentLength];
HttpPostedFile uploadedImage = UploadImage.PostedFile;
uploadedImage.InputStream.Read(imageSize, 0, (int)UploadImage.PostedFile.ContentLength);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = @"INSERT INTO table(Cols1,Cols2) VALUES (@UploadImage,@UploadText)";
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
SqlParameter UploadedImage = new SqlParameter("@UploadImage", SqlDbType.Image, imageSize.Length);
UploadedImage.Value = imageSize;
cmd.Parameters.Add(UploadedImage);
SqlParameter UploadedText = new SqlParameter("@UploadText", SqlDbType.VarChar, 200);
UploadedText.Value = strUploadPath;
cmd.Parameters.Add(UploadedText);
con.Open();
int result = cmd.ExecuteNonQuery();
con.Close();
if (result > 0)
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + strImageName + " image inserted successfully')", true);
SqlDataAdapter da = new SqlDataAdapter("select * from UploadProjectDetails", con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
}
这篇关于将文件上传到目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文