将文件上传名称添加到wwwroot文件夹后如何重命名文件并保留旧名称

时间:2019-06-24 09:31:54

标签: angular asp.net-core

我创建了具有angular和asp.net核心的文件上传控件,我需要重命名上传文件名,然后再将其添加到wwwroot中,以解决用户上传另一个同名文件时出现的问题 但是我发现在angualr和asp.net核心中FileName探测也是只读的

角度代码

server <- function(input, output, session) {

  # Reactive data base
  MBA_Online <- reactivePoll(3000, session,
                             checkFunc = function() {
                              con <- dbConnect(MySQL(),
                                               user = 'root',
                                               password = 'ruvimboML55AMG',
                                               host = 'localhost',
                                               dbname = 'healthcare_mining')

                               # get max date from database view to determine if data has been updated
                              # max_date <- dbGetQuery(con, "SELECT MAX(cu_date) AS max_cu_date
                              #                        FROM viewMaxCreatedUpdatedDates")
                              max_date_timestamp <- dbGetQuery(con, "SELECT MAX(UNIX_TIMESTAMP(date_updated)) AS last_updated
                                                     FROM onlineRetail")

                             # Convert UNIX Timestamp to date
                               max_date <- as.POSIXct(as.numeric(as.character(max_date_timestamp, tz="CAT")), origin="1970-01-01")

                               # disconnet from RDS
                              dbDisconnect(con)

                             },

                             if(max_date <= Sys.time()){

                               "No new data is available"

                             } else {

                               valueFunc = function() {
                               con <- dbConnect(MySQL(),
                                                user = 'root',
                                                password = 'ruvimboML55AMG',
                                                host = 'localhost',
                                                dbname = 'healthcare_mining')

                               # construct the SQL statement
                               sql <- "SELECT Transaction,Item, Date, Quantity, CustomerID, Amount FROM onlineRetail;"

                               dataReactive <- dbGetQuery(con, sql)

                               # disconnect
                               dbDisconnect(con)

                               return(dataReactive)

                                 }

                               #Remove negative amount values
                               MBA_Online <- MBA_Online %>%
                               filter(Amount > 0, Quantity > 0)
                               na.omit(MBA_Online, cols="CustomerID")

                               # create a directory called data, if not exists
                               if(!dir.exists("data")){dir.create("data")}

                               # write data frame as CSV
                               write.csv(MBA_Online, "data/MBA_Online.csv")
                               # write log file as csv
                               write.csv(MBA_Online, paste0("data/log_file",gsub("[^0-9]","",Sys.time()),".csv"))

                               # convert Transaction into numeric, and then arrange by Transaction ID
                               MBA_Online <- MBA_Online %>% mutate(Transaction = as.double(Transaction)) %>% arrange(Transaction)

                               # The next step is to actually convert the dataframe into basket format, based on the Transaction and Date of transaction
                               MBA_Online_itemList <- ddply(MBA_Online, c("Transaction","Date"),
                                                            function(df1)paste(df1$Item,
                                                                               collapse = ","))

                               # Once we have the transactions, we no longer need the date and member numbers in our analysis. De-select Transaction and Date
                               # then rename thee imported column
                               # then write as CSV
                               MBA_Online_itemList <- MBA_Online_itemList %>%
                               select(-Transaction, -Date)
                               MBA_Online_itemList$Quantity <- NULL
                               MBA_Online_itemList$CustomerID <- NULL
                               MBA_Online_itemList$Amount <- NULL

                               colnames(MBA_Online_itemList) <- c("itemList")

                               MBA_Online_itemList  %>% write.csv("data/MBA_OnlineItemList.csv", quote = FALSE,row.names = FALSE)

  })

}

# Run the application 
shinyApp(ui = ui, server = server)
#> Error in shinyApp(ui = ui, server = server): could not find function "shinyApp"

asp.net核心代码

  upload() {
    let selectedFile = this.uploader.queue.find(s => s.id == id);
    if (selectedFile) {
      const formData = new FormData();
      formData.append(selectedFile.file.name, selectedFile.file);
      const uploadReq = this.uploadSrv.HTTPRequestServ(
        "POST",
        this.pathAPI + `api/Upload/upload`,
        formData
      );

      this.mySubscription = this.http.request(uploadReq).subscribe(event => {
        debugger;

      });
    }
  }

1 个答案:

答案 0 :(得分:0)

在ASP.NET Core代码中,复制文件时应更改名称:

using (var stream = new FileStream(Path.Combine(newPath, "<my_file_name>"), FileMode.Create)
{
    file.CopyTo(stream);
}