Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
Education
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
黄奎
Education
Commits
63cbf5ee
Commit
63cbf5ee
authored
Jan 10, 2023
by
黄奎
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新增文件下载
parent
b06ef206
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
334 additions
and
7 deletions
+334
-7
WordTemplateHelper.cs
Edu.Common/Plugin/WordTemplateHelper.cs
+29
-0
ZipHelper.cs
Edu.Common/Plugin/ZipHelper.cs
+181
-0
QuestionController.cs
Edu.WebApi/Controllers/Course/QuestionController.cs
+124
-7
template.doc
Edu.WebApi/WordTemplate/template.doc
+0
-0
No files found.
Edu.Common/Plugin/WordTemplateHelper.cs
View file @
63cbf5ee
...
...
@@ -50,8 +50,37 @@ namespace Edu.Common.Plugin
return
null
;
}
/// <summary>
/// Url转Word文件
/// </summary>
/// <param name="url"></param>
/// <param name="templatePath"></param>
/// <param name="newFileName"></param>
/// <returns></returns>
public
static
string
UrlToWordFile
(
string
url
,
string
templatePath
,
string
newFilePath
,
string
newFileName
)
{
try
{
var
byteData
=
UrlToWord
(
url
,
templatePath
);
if
(!
Directory
.
Exists
(
newFilePath
))
{
Directory
.
CreateDirectory
(
newFilePath
);
}
string
filePath
=
Path
.
Combine
(
newFilePath
,
newFileName
);
FileStream
fileStream
=
new
FileStream
(
filePath
,
FileMode
.
Create
);
fileStream
.
Write
(
byteData
,
0
,
byteData
.
Length
);
fileStream
.
Close
();
}
catch
{
}
finally
{
}
return
null
;
}
#
endregion
}
...
...
Edu.Common/Plugin/ZipHelper.cs
0 → 100644
View file @
63cbf5ee
using
ICSharpCode.SharpZipLib.Checksum
;
using
ICSharpCode.SharpZipLib.Zip
;
using
System
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.Text
;
namespace
Edu.Common.Plugin
{
/// <summary>
/// Zip帮助类
/// </summary>
public
class
ZipHelper
{
#
region
zip
压缩
/// <summary>
/// 压缩文件或文件夹 ----无密码
/// </summary>
/// <param name="fileToZip">要压缩的路径-文件夹或者文件</param>
/// <param name="zipedFile">压缩后的文件名</param>
/// <param name="errorOut">如果失败返回失败信息</param>
/// <returns>压缩结果</returns>
public
static
bool
Zip
(
string
fileToZip
,
string
zipedFile
,
ref
string
errorOut
)
{
Encoding
.
RegisterProvider
(
CodePagesEncodingProvider
.
Instance
);
bool
result
=
false
;
try
{
if
(
Directory
.
Exists
(
fileToZip
))
result
=
ZipDirectory
(
fileToZip
,
zipedFile
,
null
);
else
if
(
File
.
Exists
(
fileToZip
))
result
=
ZipFile
(
fileToZip
,
zipedFile
,
null
);
}
catch
(
Exception
ex
)
{
LogHelper
.
Write
(
ex
,
"Zip"
);
errorOut
=
ex
.
Message
;
}
return
result
;
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="fileToZip">要压缩的文件全名</param>
/// <param name="zipedFile">压缩后的文件名</param>
/// <param name="password">密码</param>
/// <returns>压缩结果</returns>
private
static
bool
ZipFile
(
string
fileToZip
,
string
zipedFile
,
string
password
)
{
bool
result
=
true
;
ZipOutputStream
zipStream
=
null
;
FileStream
fs
=
null
;
ZipEntry
ent
=
null
;
if
(!
File
.
Exists
(
fileToZip
))
return
false
;
try
{
fs
=
File
.
OpenRead
(
fileToZip
);
byte
[]
buffer
=
new
byte
[
fs
.
Length
];
fs
.
Read
(
buffer
,
0
,
buffer
.
Length
);
fs
.
Close
();
fs
=
File
.
Create
(
zipedFile
);
zipStream
=
new
ZipOutputStream
(
fs
);
if
(!
string
.
IsNullOrEmpty
(
password
))
zipStream
.
Password
=
password
;
ent
=
new
ZipEntry
(
Path
.
GetFileName
(
fileToZip
));
zipStream
.
PutNextEntry
(
ent
);
zipStream
.
SetLevel
(
6
);
zipStream
.
Write
(
buffer
,
0
,
buffer
.
Length
);
}
catch
(
Exception
ex
)
{
result
=
false
;
throw
ex
;
}
finally
{
if
(
zipStream
!=
null
)
{
zipStream
.
Finish
();
zipStream
.
Close
();
}
if
(
ent
!=
null
)
{
ent
=
null
;
}
if
(
fs
!=
null
)
{
fs
.
Close
();
fs
.
Dispose
();
}
}
GC
.
Collect
();
GC
.
Collect
(
1
);
return
result
;
}
/// <summary>
/// 压缩文件夹
/// </summary>
/// <param name="strFile">带压缩的文件夹目录</param>
/// <param name="strZip">压缩后的文件名</param>
/// <param name="password">压缩密码</param>
/// <returns>是否压缩成功</returns>
private
static
bool
ZipDirectory
(
string
strFile
,
string
strZip
,
string
password
)
{
bool
result
=
false
;
if
(!
Directory
.
Exists
(
strFile
))
return
false
;
if
(
strFile
[
strFile
.
Length
-
1
]
!=
Path
.
DirectorySeparatorChar
)
strFile
+=
Path
.
DirectorySeparatorChar
;
ZipOutputStream
s
=
new
ZipOutputStream
(
File
.
Create
(
strZip
));
s
.
SetLevel
(
6
);
if
(!
string
.
IsNullOrEmpty
(
password
))
s
.
Password
=
password
;
try
{
result
=
Zip
(
strFile
,
s
,
strFile
);
}
catch
(
Exception
ex
)
{
throw
ex
;
}
finally
{
s
.
Finish
();
s
.
Close
();
}
return
result
;
}
/// <summary>
/// 压缩文件夹内部方法
/// </summary>
/// <param name="strFile"></param>
/// <param name="s"></param>
/// <param name="staticFile"></param>
/// <returns></returns>
private
static
bool
Zip
(
string
strFile
,
ZipOutputStream
s
,
string
staticFile
)
{
bool
result
=
true
;
if
(
strFile
[
strFile
.
Length
-
1
]
!=
Path
.
DirectorySeparatorChar
)
strFile
+=
Path
.
DirectorySeparatorChar
;
Crc32
crc
=
new
Crc32
();
try
{
string
[]
filenames
=
Directory
.
GetFileSystemEntries
(
strFile
);
foreach
(
string
file
in
filenames
)
{
if
(
Directory
.
Exists
(
file
))
{
Zip
(
file
,
s
,
staticFile
);
}
else
// 否则直接压缩文件
{
//打开压缩文件
FileStream
fs
=
File
.
OpenRead
(
file
);
byte
[]
buffer
=
new
byte
[
fs
.
Length
];
fs
.
Read
(
buffer
,
0
,
buffer
.
Length
);
string
tempfile
=
file
.
Substring
(
staticFile
.
LastIndexOf
(
"\\"
)
+
1
);
ZipEntry
entry
=
new
ZipEntry
(
tempfile
)
{
DateTime
=
DateTime
.
Now
,
Size
=
fs
.
Length
};
fs
.
Close
();
crc
.
Reset
();
crc
.
Update
(
buffer
);
entry
.
Crc
=
crc
.
Value
;
s
.
PutNextEntry
(
entry
);
s
.
Write
(
buffer
,
0
,
buffer
.
Length
);
}
}
}
catch
(
Exception
ex
)
{
result
=
false
;
throw
ex
;
}
return
result
;
}
#
endregion
}
}
Edu.WebApi/Controllers/Course/QuestionController.cs
View file @
63cbf5ee
...
...
@@ -12,7 +12,9 @@ using Microsoft.AspNetCore.Cors;
using
Microsoft.AspNetCore.Mvc
;
using
System
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.Linq
;
using
System.Net
;
using
System.Text.RegularExpressions
;
using
System.Threading.Tasks
;
...
...
@@ -1328,26 +1330,141 @@ namespace Edu.WebApi.Controllers.Course
/// <returns></returns>
[
HttpPost
]
[
Obsolete
]
public
FileContent
Result
DownLoadBank
()
public
Api
Result
DownLoadBank
()
{
List
<
ExcelDataSource
>
slist
=
new
List
<
ExcelDataSource
>()
;
string
messageTip
=
""
;
string
BankIds
=
base
.
ParmJObj
.
GetStringValue
(
"BankId"
);
string
basepath
=
AppContext
.
BaseDirectory
;
string
zipFileName
=
DateTime
.
Now
.
Ticks
+
".zip"
;
string
fileName
=
DateTime
.
Now
.
Ticks
+
".doc"
;
string
filoderName
=
string
.
Format
(
BankIds
+
"_"
+
DateTime
.
Now
.
ToString
(
"yyyyMMddhhmmssfff"
));
string
savePath
=
basepath
+
string
.
Format
(
"\\upfile\\Temporary\\{0}\\"
,
filoderName
);
string
savePath2
=
basepath
+
string
.
Format
(
"\\upfile\\Temporary\\{0}_Zip\\"
,
filoderName
);
string
fileUrl
=
"/upfile/Temporary/"
+
filoderName
+
"/"
+
zipFileName
;
try
{
if
(!
Directory
.
Exists
(
savePath
))
{
Directory
.
CreateDirectory
(
savePath
);
}
if
(!
Directory
.
Exists
(
savePath2
))
{
Directory
.
CreateDirectory
(
savePath2
);
}
string
url
=
Common
.
Config
.
UploadSiteUrl
+
"/Home/JieHeQuestion?showType=1&BankIds="
+
BankIds
;
string
basepath
=
AppContext
.
BaseDirectory
;
string
tempPath
=
basepath
+
"\\upfile\\Template\\"
;
string
path_server
=
tempPath
+
"template.doc"
;
var
byteData
=
Common
.
Plugin
.
WordTemplateHelper
.
UrlToWord
(
url
,
path_server
);
return
File
(
byteData
,
"application/octet-stream"
,
fileName
);
string
iframeRule
=
@"<iframe(([\s\S])*?)<\/iframe>"
;
var
listenList
=
questionModule
.
GetQuestionListModule
(
new
RB_Question_ViewModel
()
{
QBankIds
=
BankIds
,
QCategoryId
=
"9"
})?.
OrderBy
(
qitem
=>
qitem
.
QuestionId
)?.
ToList
();
//获取音频文件
List
<
FileItem
>
fileList
=
new
List
<
FileItem
>();
if
(
listenList
!=
null
&&
listenList
.
Count
>
0
)
{
int
index
=
0
;
foreach
(
var
item
in
listenList
)
{
try
{
MatchCollection
iframe_matchs
=
Regex
.
Matches
(
item
.
Title
,
iframeRule
);
foreach
(
Match
subItem
in
iframe_matchs
)
{
string
newUrl
=
""
;
var
tempArray
=
subItem
.
Value
.
Split
(
new
char
[]
{
' '
,
' '
});
foreach
(
var
tItem
in
tempArray
)
{
if
(
tItem
.
IndexOf
(
"src"
)
!=
-
1
)
{
var
imageArray
=
tItem
.
Split
(
new
string
[]
{
"url="
},
StringSplitOptions
.
None
);
if
(
imageArray
!=
null
&&
imageArray
.
Length
>
0
&&
imageArray
.
Length
==
2
)
{
string
tempUrl
=
imageArray
[
1
].
Substring
(
0
,
imageArray
[
1
].
Length
-
1
);
newUrl
=
System
.
Web
.
HttpUtility
.
UrlDecode
(
tempUrl
);
}
}
}
string
key
=
"[音频"
+
index
+
"]"
;
fileList
.
Add
(
new
FileItem
()
{
FileName
=
key
,
FileUrl
=
newUrl
,
});
index
++;
}
}
catch
{
}
if
(!
string
.
IsNullOrEmpty
(
item
.
QuestionContent
))
{
var
subList
=
Common
.
Plugin
.
JsonHelper
.
DeserializeObject
<
List
<
readingComprehensioItem
>>(
item
.
QuestionContent
);
if
(
subList
!=
null
&&
subList
.
Count
>
0
)
{
foreach
(
var
subItem
in
subList
)
{
string
newSubTitle
=
subItem
.
SubTitle
;
try
{
MatchCollection
iframe_matchs
=
Regex
.
Matches
(
newSubTitle
,
iframeRule
);
foreach
(
Match
iframeItem
in
iframe_matchs
)
{
string
newUrl
=
""
;
var
tempArray
=
iframeItem
.
Value
.
Split
(
new
char
[]
{
' '
,
' '
});
foreach
(
var
tItem
in
tempArray
)
{
if
(
tItem
.
IndexOf
(
"src"
)
!=
-
1
)
{
var
imageArray
=
tItem
.
Split
(
new
string
[]
{
"url="
},
StringSplitOptions
.
None
);
if
(
imageArray
!=
null
&&
imageArray
.
Length
>
0
&&
imageArray
.
Length
==
2
)
{
string
tempUrl
=
imageArray
[
1
].
Substring
(
0
,
imageArray
[
1
].
Length
-
1
);
newUrl
=
System
.
Web
.
HttpUtility
.
UrlDecode
(
tempUrl
);
}
}
}
string
key
=
"[音频"
+
index
+
"]"
;
newSubTitle
=
newSubTitle
.
Replace
(
iframeItem
.
Value
,
key
);
fileList
.
Add
(
new
FileItem
()
{
FileName
=
key
,
FileUrl
=
newUrl
,
});
index
++;
}
}
catch
{
}
}
item
.
QuestionContent
=
Common
.
Plugin
.
JsonHelper
.
Serialize
(
subList
);
}
}
}
}
var
wordFile
=
Common
.
Plugin
.
WordTemplateHelper
.
UrlToWordFile
(
url
,
path_server
,
savePath
,
fileName
);
foreach
(
var
item
in
fileList
)
{
if
(!
string
.
IsNullOrEmpty
(
item
.
FileUrl
))
{
WebClient
mywebclient
=
new
WebClient
();
mywebclient
.
DownloadFile
(
item
.
FileUrl
,
savePath
+
item
.
FileName
+
".mp3"
);
mywebclient
.
Dispose
();
}
}
Common
.
Plugin
.
ZipHelper
.
Zip
(
savePath
,
savePath2
+
zipFileName
,
ref
messageTip
);
}
catch
(
Exception
ex
)
{
LogHelper
.
Write
(
ex
,
"DownLoadBank"
);
var
byteData1
=
ExcelTempLateHelper
.
ToExcelExtend
(
slist
);
return
File
(
byteData1
,
"application/octet-stream"
,
fileName
);
}
return
ApiResult
.
Success
(
data
:
fileUrl
);
}
}
}
\ No newline at end of file
Edu.WebApi/WordTemplate/template.doc
0 → 100644
View file @
63cbf5ee
File added
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment