1

我有要求,我有带有文件上传器和按钮名称的 asp 页面"upload"......
不使用浏览器,我想在文件上传器中加载一些默认文件,假设我有文件夹c:\serverfloder并且我有图像"image1.jpg"......当用户请求时1次,我想automatically load the file into file uploader ..
根据用户要求,我们有机会更改文件文件....我不想限制用户。

4

1 回答 1

0

您可以参考我为我的应用程序编写的以下代码

 string path = FileUpload1.PostedFile.FileName.ToString();
        string FileName = FileUpload1.FileName.ToString();
        string FileSize = FileUpload1.PostedFile.ContentLength.ToString();
        string FileContent = FileUpload1.PostedFile.ContentType.ToString();
        string filename = Path.GetFileName(path);
        FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fs);
        Byte[] bytes = br.ReadBytes((Int32)fs.Length);
        br.Close();
        fs.Close();
        string strQuery = "INSERT INTO [testdb1].[dbo].[tblAttachment]([f_name],[fileSize],[attachment],[contentType]) VALUES(@f_name,@fileSize,@attachment,@contentType)";
        SqlCommand cmd = new SqlCommand(strQuery);
        cmd.Parameters.Add("@f_name", SqlDbType.VarChar).Value = FileName;
        cmd.Parameters.Add("@fileSize", SqlDbType.VarChar).Value = Convert.ToInt32(FileSize);
        cmd.Parameters.Add("@attachment", SqlDbType.Binary).Value = bytes;
        cmd.Parameters.Add("@contentType", SqlDbType.VarChar).Value = FileContent;

并访问此处以获取C# 中的文件操作 ,它们将是其他将图像直接存储到特定文件夹的方法,或者尝试这个..将直接将文件保存到文件夹

 if (FileUpload1.HasFile)

  {
            FileUpload1.SaveAs(@"D:\MVC Application Demos\FileOpearationsDemo\FileOpearationsDemo\Files\" + FileUpload1.FileName);

        }
于 2013-05-27T09:29:32.707 回答