简体   繁体   中英

saving a copy of a file being uploaded

I have a method that is uploading a file and at the same time I am changing the name and path and saving a copy of it to a new path with a new name.

In the database I could see the new file name and path but when I go to the actual folder where the new file should be saved it isn't there. If anyone could point out how I could fix this that would be great :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);
}

I use this code in one of my projects:

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);
    }
}

and finally save filePath1 in your DB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM