保存正在上传的文件的副本

时间:2013-07-01 13:33:20

标签: c# asp.net-mvc

我有一个上传文件的方法,同时我正在更改名称和路径,并将其副本保存到具有新名称的新路径。

在数据库中,我可以看到新的文件名和路径,但是当我转到应保存新文件的实际文件夹时,它不在那里。如果有人能指出我如何解决这个问题会很棒:D

static string docx = @"../../TestFiles/Types.docx";

try
{
    FileStream fileStream = new FileStream(docx, FileMode.Open, FileAccess.Read);
    string filePath = fileStream.Name;
    string fileName = Path.GetFileName(fileStream.Name);
    docConverter.FileSubmit(6, filePath, fileName, 1, FILESTORELOCATION, 89);
    fileStream.Close();
}

public void FileSubmit(int studentId, string rawStoragePath, string fileName, int uploadedByUserId, string storagePath, int assignmentElementsId)
{
    Guid strGUID = Guid.NewGuid();
    string FILESTORELOCATION = @"C:\TestFiles\";
    storagePath = FILESTORELOCATION + strGUID + ".pdf";
    DateTime uploadDate = DateTime.UtcNow;

    stuSubSvc.UploadAssignment(studentId, strGUID,fileName, rawStoragePath, uploadDate, uploadedByUserId, storagePath, 0, assignmentElementsId);
}

1 个答案:

答案 0 :(得分:0)

我在我的一个项目中使用此代码:

string consPath = "C:\\WEB\\DirectiveFiles\\";

private string FindExtension(string FileFullName)
{

    int lastDOT = FileFullName.LastIndexOf(".");
    string myFileExtension = FileFullName.Substring(lastDOT + 1);
    myFileExtension = myFileExtension.ToLower();
    if (myFileExtension != "jpg" && myFileExtension != "pdf" && myFileExtension != "gif" && myFileExtension != "zip")
        myFileExtension = "Error";

    return myFileExtension;
}

if (fileUp1.HasFile)
{
    fileExtension = FindExtension(fileUp1.FileName);
    if (fileExtension == "Error")
    {
        extFlag = true;
        lblUpError1.Visible = true;
        lblUpError1.Text = "Not Proper File " + fileUp1.FileName;
        goto continueWithoutUpload;
    }
    else
    {
        filePath1 = consPath + DocID + "." + fileExtension;
        fileUp1.SaveAs(consPath + fileUp1.FileName);
        File.Move(@consPath + fileUp1.FileName, @filePath1);
    }
}

最后在数据库中保存filePath1