将文件路径写入sql [英] writing path of file into sql

查看:113
本文介绍了将文件路径写入sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够使用以下方法上传asp.net中的多个文件 [ ^ ]成功上传文件,但我无法弄清楚如何编写文件名和sql路径。我尝试过获取文件名,但只返回选中的最后一个文件而不是所有文件。





I have been able to use the following method Upload multiple files in asp.net[^]to upload files successfully, but i haven't been able to figure out how to write file(s) name and path to sql. I have tried getting file names but it only returns with last file selected and not all files.


string filesname = System.IO.Path.GetFileName(hpf1.FileName);







我无法计算如何获取所有文件选择的名称。



请帮助。



每条需求:






I have not be able to figure out how to get all files selected names.

Please help.

Per Req:

try
{
    HttpFileCollection hfc1 = Request.Files;
    for (int i = 0; i < hfc1.Count; i++)
    {
        string ponoref = txtRequsitionNo.Text;

        String pathref = Server.MapPath("~/uploads/" + ponoref + "/");

        if (!System.IO.Directory.Exists(pathref))
        {
            System.IO.Directory.CreateDirectory(pathref);
        }

        HttpPostedFile hpf1 = hfc1[i];
        if (hpf1.ContentLength > 0)
        {
            hpf1.SaveAs(Server.MapPath("~/uploads/") + ponoref + @"/" + System.IO.Path.GetFileName(hpf1.FileName));
        }
        string filesname = System.IO.Path.GetFileName(hpf1.FileName);

        string POREF = @"uploads\" + ponoref + @"\" + filesname;

        powholeref.Text = POREF.ToString();
    }
}
catch (Exception)
{

    throw;
}

txtRequsitionNo.Text = string.Empty;

推荐答案

您正在将所有文件名分配到一个变量中。当您查看for循环中的文件时,它总是被当前文件名覆盖,并且在退出循环后自然会有最后一个。你需要的是一个列表。

You are assigning all the file names into one variable. As you go over the files in the for-loop this is always overwritten by the current file name and naturally after you exit the loop there is the last one. What you need is a list.
var listOfFiles = new List<string>(hfc1.Count);
for (int i = 0; i < hfc1.Count; i++)
{
    // ...

    string filesname = System.IO.Path.GetFileName(hpf1.FileName);
    listOfFiles.Add(filesname);

    // ...
}

// do something with listOfFiles


不要在快速答案下发布 - 如果您从文章中获得了代码,那么该文章底部会出现添加评论或问题按钮,这会导致发送电子邮件给作者。然后他们会被告知你希望与他们交谈。

在这里发布这个依赖于他们匆匆而过并意识到这是为了他们。
Don't post this under Quick Answers - if you got the code from an article, then there is a "Add a Comment or Question" button at the bottom of that article, which causes an email to be sent to the author. They are then alerted that you wish to speak to them.
Posting this here relies on them "dropping by" and realising it is for them.


这篇关于将文件路径写入sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆