-
Notifications
You must be signed in to change notification settings - Fork 61
feat: support to upload file from file manger #542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| <script setup lang="ts"> | ||
| import { ref, onMounted } from 'vue' | ||
| import { ElMessage } from 'element-plus' | ||
| const fileLists = ref([]) | ||
| onMounted(() => { | ||
| const savedFile = localStorage.getItem("fileLists") | ||
| if (savedFile) { | ||
| fileLists.value = JSON.parse(savedFile) | ||
| } | ||
| }) | ||
| const handleFileUpload = (file) => { | ||
| const reader = new FileReader() | ||
| reader.onload = (e: any) => { | ||
| const fileData = { | ||
| name: file.name, | ||
| type: file.type, | ||
| size: file.size, | ||
| content: e.target.result // 文件内容作为 Base64 字符串 | ||
| } | ||
| fileLists.value.push(fileData) // 将文件信息推入fileList | ||
| localStorage.setItem('fileLists', JSON.stringify(fileLists.value)) // 存入localStorage | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to move it into And please write the comment with English. |
||
| ElMessage({ | ||
| message: '文件上传成功', | ||
| type: 'success' | ||
| }) | ||
| } | ||
| reader.readAsDataURL(file.raw) // 读取文件内容为Base64编码 | ||
| return false | ||
| } | ||
| const deleteFile = (file) => { | ||
| const index = fileLists.value.findIndex(item => item.name === file.name) | ||
| if (index !== -1) { | ||
| fileLists.value.splice(index, 1) | ||
| localStorage.setItem('fileLists', JSON.stringify(fileLists.value)) | ||
| } | ||
| } | ||
| </script> | ||
|
|
||
| <template> | ||
| <div> | ||
| <h2>文件管理器</h2> | ||
|
|
||
| <el-upload | ||
| action="#" | ||
| :auto-upload="false" | ||
| :on-change="handleFileUpload" | ||
| :on-remove="deleteFile" | ||
| :file-list="fileLists" | ||
| multiple | ||
| > | ||
| <el-button type="primary">点击上传</el-button> | ||
| <template #tip> | ||
| <div class="el-upload__tip"> | ||
| 可以上传任意类型的文件 | ||
| </div> | ||
| </template> | ||
| </el-upload> | ||
|
|
||
| </div> | ||
| </template> | ||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -242,7 +242,8 @@ const emptyTestCaseWithSuite: TestCaseWithSuite = { | |||
| cookie: [], | ||||
| form: [], | ||||
| body: '', | ||||
| filepath: '' | ||||
| filepath: '', | ||||
| filename: '' | ||||
| }, | ||||
| response: { | ||||
| statusCode: 0, | ||||
|
|
@@ -337,7 +338,6 @@ function determineBodyType(e: TestCase) { | |||
| break | ||||
| case 'multipart/form-data': | ||||
| bodyType.value = 6 | ||||
|
|
||||
| e.request.form.forEach(fItem => { | ||||
| if (fItem.key !== '' && fItem.key !== '') { | ||||
| e.request.filepath = fItem.key + "=" + fItem.value | ||||
|
|
@@ -634,6 +634,7 @@ const deleteAllHistory = async (formEl) => { | |||
| } | ||||
|
|
||||
| const options = GetHTTPMethods() | ||||
| const fileLists = ref([]) | ||||
| const requestActiveTab = ref(Cache.GetPreference().requestActiveTab) | ||||
| watch(requestActiveTab, Cache.WatchRequestActiveTab) | ||||
| Magic.Keys(() => { | ||||
|
|
@@ -721,6 +722,22 @@ function formChange() { | |||
| } | ||||
| } | ||||
|
|
||||
| function handleSelectChange() { | ||||
| const items = testCaseWithSuite.value.data.request.filename | ||||
| if (items) { | ||||
| testCaseWithSuite.value.data.request.form = [{ | ||||
| key: "file", | ||||
| value: items | ||||
| } as Pair] | ||||
|
|
||||
| const file = fileLists.value.find((e) => e.value === items) | ||||
| if (file) { | ||||
| const content = file.content.split(',')[1] | ||||
| testCaseWithSuite.value.data.request.body = atob(content) | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can keep it as the base64 encoded content. The below function could decode it on the server side. api-testing/pkg/testing/case.go Line 186 in 3a9bde8
The decoded content might have many invisible characters. See also |
||||
| } | ||||
| } | ||||
| } | ||||
|
|
||||
| const filepathChange = () => { | ||||
| const items = testCaseWithSuite.value.data.request.filepath.split("=") | ||||
| if (items && items.length > 1) { | ||||
|
|
@@ -730,6 +747,7 @@ const filepathChange = () => { | |||
| } as Pair] | ||||
| } | ||||
| } | ||||
|
|
||||
| const bodyType = ref(1) | ||||
| function bodyTypeChange(e: number) { | ||||
| let contentType = "" | ||||
|
|
@@ -744,6 +762,20 @@ function bodyTypeChange(e: number) { | |||
| contentType = 'multipart/form-data' | ||||
| filepathChange() | ||||
| break; | ||||
| case 7: | ||||
| // 获取localStorage中的数据 | ||||
| const savedFiles = localStorage.getItem('fileLists') | ||||
| if (savedFiles) { | ||||
| const fileList = JSON.parse(savedFiles) | ||||
| console.log(fileList) | ||||
| fileLists.value = fileList.map(file => ({ | ||||
| label: file.name, | ||||
| value: file.name, | ||||
| content: file.content, | ||||
| disabled: false | ||||
| })) | ||||
| } | ||||
| break; | ||||
| } | ||||
|
|
||||
| if (contentType !== "") { | ||||
|
|
@@ -1020,9 +1052,23 @@ Magic.Keys(() => { | |||
| <el-radio :label="4">x-www-form-urlencoded</el-radio> | ||||
| <el-radio :label="5">JSON</el-radio> | ||||
| <el-radio :label="6">EmbedFile</el-radio> | ||||
| <el-radio-button :label="7">UploadFile</el-radio-button> | ||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to use the same element |
||||
| </el-radio-group> | ||||
|
|
||||
| <div style="flex-grow: 1;"> | ||||
| <el-select v-if="bodyType === 7" | ||||
| v-model="testCaseWithSuite.data.request.filename" | ||||
| @change="handleSelectChange" | ||||
| placeholder="Select" | ||||
| style="width: 240px"> | ||||
| <el-option | ||||
| v-for="item in fileLists" | ||||
| :key="item.value" | ||||
| :label="item.label" | ||||
| :value="item.value" | ||||
| :disabled="item.disabled" | ||||
| /> | ||||
| </el-select> | ||||
| <div v-if="bodyType === 6"> | ||||
| <el-row> | ||||
| <el-col :span="4">Filename:</el-col> | ||||
|
|
||||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest giving it a file size limit. For example:
10k.