System.IO.FileNotFoundExecptio ... [英] System.IO.FileNotFoundExecptio...

查看:100
本文介绍了System.IO.FileNotFoundExecptio ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用GUID来允许用户上传具有相同名称的文件,但GUID附加到文件名& amp; 存储在数据库中的文件路径与附加到服务器上存储的文件的GUID不同。这意味着它为同一个文件提供了两个不同的GUID。因此,当我尝试从服务器下载文件时,它给我一个FileNotFound异常,因为命令arguement从数据库获取文件名,因此当它进入服务器时它无法找到该文件作为存储在该服务器上的同一文件的GUID服务器是不同的。如果有人能给我一些值得注意的建议,请给我一些

我的数据库架构:

< pre lang = sql > 表:Sales
列:ReceiptFileName (存储没有GUID的名称)
Coulmn:ReceiptFilePath(使用GUID存储文件路径)
列:filename(使用GUID存储文件名)< < span class =code-leadattribute> / pre >





Aspx代码:

 <   ItemTemplate  >  
< asp:LinkBut​​ton ID = LinkBut​​ton1 runat = server CommandName = 下载 CommandArgument =' <% #Bind( filename%> ' 文字 =' < span class =code-keyword> <% #Bind( ReceiptFileName%> ' > < / asp:LinkBut​​ton >
< / ItemTemplate >







上传代码:

  if (FileUpload1.HasFile)
{
// check file Extension&大小
string filename = FileUpload1.PostedFile.FileName;

{
filename = filename + Guid.NewGuid();
}

int filesize = FileUpload1.PostedFile.ContentLength;

if (filesize > 20 * 1024 ))
{
Label1.Text = 请上传zip或pdf文件;
}

string fileextention = System.IO.Path.GetExtension(FileUpload1.FileName);

if (fileextention.ToLower()!= 。zip&& fileextention.ToLower()!= 。 pdf
{
Label1.ForeColor = System.Drawing.Color.Green;
Label1.Text = 请上传zip或pdf文件;
}
其他
{
// < span class =code-comment> string ReceiptFileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string ReceiptFileName = Path。用GetFileName(文件名);

// 将文件保存到磁盘
FileUpload1.SaveAs( Server.MapPath( 〜/ Reimbursement / Reciepts / + ReceiptFileName));
}
}





添加文件名和收据名的方法

  public   static   int  AddSale(SaleID,字符串 ReceiptFileName,字符串 ReceiptFilePath, string  filename)
{
int sale;
SqlCommand addSales = new SqlCommand( addSale ,con);

addSale.CommandType = CommandType.StoredProcedure;
addSale.Parameters.AddWithValue( @ ReceiptFileName,ReceiptFileName);
addSale.Parameters.AddWithValue( @ filename,filename);
addSale.Parameters.AddWithValue( @ ReceiptFilePath,ReceiptFilePath);

尝试
{
con.Open();
added = addSale.ExecuteNonQuery();
}

最后
{
con.Close();
}











代码下载:

 受保护  void  gridExpenditures_RowCommand( object  sender,GridViewCommandEventArgs e)
{
if (e .CommandName == 下载
{
Response.Clear() ;
Response.ContentType = application / octet-stream;
Response.AppendHeader( content-disposition FileName = + e.CommandArgument);
Response.TransmitFile(Server.MapPath( 〜/ Reimbursement / Reciepts /)+ e.CommandArgument);
Response.End();
}
}

解决方案

只是一个建议。

如果可以添加数据库中的一列,为什么不添加类型为VARCHAR或GUID的FileID,并将FileID作为主键。

它不是直接解决您的实际问题,而是更多不同的方法。

I am trying to use GUID to allow users to upload files with same names, but the GUIDs attach to filenames &amp; filepaths stored in database are different from the GUIDs attached to the files stored on server. It means it is giving two different GUIDs to the same file. So when I am trying to download the file from server its giving me an FileNotFound exception as the command arguement takes the filename from database and therefore when it goes to server its not able to find the file as the GUID of the same file stored on the server is different. Please if anybody can give me some advise that would be appreciable

My Database schema:

<pre lang="sql">Table: Sales
Columns: ReceiptFileName(Stores Name without GUID)
Coulmn:ReceiptFilePath(Stores file path with GUID)
Column: filename(Stores filename with GUID)</pre>



Aspx code:

<ItemTemplate>
     <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Download" CommandArgument='<%# Bind("filename") %>' Text='<%# Bind("ReceiptFileName") %>' ></asp:LinkButton>
</ItemTemplate>




Code for upload:

if (FileUpload1.HasFile)
{
    //check file Extension & Size
    string filename = FileUpload1.PostedFile.FileName;

    {
        filename = filename + Guid.NewGuid();
    }

    int filesize = FileUpload1.PostedFile.ContentLength;

    if (filesize > (20 * 1024))
    {
        Label1.Text = "Please upload a zip or a pdf file";
    }

    string fileextention = System.IO.Path.GetExtension(FileUpload1.FileName);

    if (fileextention.ToLower() != ".zip" && fileextention.ToLower() != ".pdf")
    {
        Label1.ForeColor = System.Drawing.Color.Green;
        Label1.Text = "Please upload a zip or a pdf file";
    }
    else
    {
        //string ReceiptFileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
        string ReceiptFileName = Path.GetFileName(filename);

        //save file to disk
        FileUpload1.SaveAs(Server.MapPath("~/Reimbursement/Reciepts/" + ReceiptFileName));
    }
}



Method for adding filename and receiptname

public static int AddSale(SaleID,  string ReceiptFileName,string ReceiptFilePath, string filename)
{
    int sale;
    SqlCommand addSales = new SqlCommand("addSale", con);

    addSale.CommandType = CommandType.StoredProcedure;
    addSale.Parameters.AddWithValue("@ReceiptFileName", ReceiptFileName);
    addSale.Parameters.AddWithValue("@filename", filename);
    addSale.Parameters.AddWithValue("@ReceiptFilePath", ReceiptFilePath);

    try
    {
        con.Open();
        added = addSale.ExecuteNonQuery();
    }

    finally
    {
        con.Close();
    }






Code for download:

protected void gridExpenditures_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Download")
    {
        Response.Clear();
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("content-disposition", "FileName=" + e.CommandArgument);
        Response.TransmitFile(Server.MapPath("~/Reimbursement/Reciepts/") + e.CommandArgument);
        Response.End();
    }
}

解决方案

Just a suggestion.
If you can add a column in your database, why not add a FileID with the type VARCHAR or GUID and make FileID the primary key.
It is not a direct solution to your actual problem, but more of a different approach.


这篇关于System.IO.FileNotFoundExecptio ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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