如何在本地系统上上传文件 [英] how to upload a files on local system
本文介绍了如何在本地系统上上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在c#窗口应用程序中使用文本框,浏览按钮和上传按钮...
我在文本框中得到了文件路径...但是我单击了上载按钮,它显示了一些错误.........
按钮浏览代码
I am using text box,browse button and upload button in c# window application...
i get the file path in text box...but i click the upload button its shows some error.........
Button browse code
private void btnbrowse_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = ofd.FileName;
}
}
按钮上传代码
Button upload code
private void btnupload_Click(object sender, EventArgs e)
{
HttpFileCollection uploads = HttpContext.Current.Request.Files;
for (int i = 0; i < uploads.Count; i++)
{
//HttpPostedFile upload = uploads[i];
if (upload.ContentLength == 0)
continue;
string c = lbl1filename.Text.ToString();
//string filename = System.IO.Path.GetFileNameWithoutExtension(upload.FileName);
//string extension = System.IO.Path.GetExtension(upload.FileName);
string nfilepath;
nfilepath = "D:\\saravana\\Spatiotemporal Approach\\SourceMain\\source_uploadfiles\\" + c;
try
{
upload.SaveAs("D:\\saravana\\Spatiotemporal Approach\\SourceMain\\source_uploadfiles\\" + c);
//Span1.InnerHtml = "Upload(s) Successful.";
}
catch (Exception Exp)
{
//Span1.InnerHtml = "Upload(s) FAILED.";
Exp.ToString();
}
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into filedetails(filename,filetype,filepath) values('" + lbl1filename.Text + "','" + nfilepath + "')";
if (con.State == ConnectionState.Closed)
{
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Uploaded suxcessfully");
}
if (con.State == ConnectionState.Open)
con.Close();
}
}
推荐答案
尝试通过代码进行调试.
请注意您在哪里得到错误.
检查文件是否存在于硬编码的文件路径中.
Try debugging through the code.
Note where you get the error.
Check if the file exists at the hardcoded file path.
if (FileUploadControl.HasFile)
{
try
{
filename = Path.GetFileName(FileUploadControl.FileName);
FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
objInwardLetter.strPath = "~/" + filename;
}
赶上
{}
}
}
catch
{}
}
这篇关于如何在本地系统上上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文