我在铁轨上的红宝石很新.我遇到了问题.我想创建一个文件上传功能,通过它我可以上传任何类型的文件(文本,图像等).我的控制器文件是(upload_controller.rb):
class UploadController < ApplicationController
def index
render :file => 'app\views\upload\uploadfile.html.erb'
end
def uploadFile
post = DataFile.save(params[:upload])
render :text => "File has been uploaded successfully"
end
end
Run Code Online (Sandbox Code Playgroud)
我的模型文件是(data_file.rb):
class DataFile < ActiveRecord::Base
attr_accessor :upload
def self.save(upload)
name = upload['datafile'].original_filename
directory = 'public/data'
# create the file path
path = File.join(directory,name)
# write the file
File.open(path, "wp") { |f| f.write(upload['datafile'].read)}
end
end
Run Code Online (Sandbox Code Playgroud)
我的View文件是(uploadfile.html.erb):
<h1>File Upload</h1>
<%= form_tag({:action => 'uploadFile'}, :multipart => true) do %>
<p><label for="upload_file">Select File</label>
<%= file_field 'upload', 'datafile' …Run Code Online (Sandbox Code Playgroud) 我取一个带有整数计数的字符串
int count=0;
string s="wow"
Run Code Online (Sandbox Code Playgroud)
我正在使用 foreach 循环来计算指定字符串中的字符数
foreach(char ch in s)
{
count++
}
Run Code Online (Sandbox Code Playgroud)
那么我如何计算在我的字符串中重复的那些字符,比如'w'。